楽天用URLメーカー
ポイントは
URLエンコード
set encodeURLText to my encodeURL(ReplaceText) as Unicode text
-----URLエンコードのサブルーチン
on encodeURL(str)
set scpt to "php -r 'echo urlencode(\"" & str & "\");'"
return do shell script scpt
end encodeURL
この部分になります。
http://plaza.rakuten.co.jp/
↑
この形式のURLを
このような形式
↓
http%3A%2F%2Fplaza.rakuten.co.jp%2F
に変換しているわけです。
OSXには標準でphpが利用可能になっていますので
phpの『urlencode』を使って変換しているわけです。
↓のXXXXXには楽天のアフィリエイトIDを入力する部分になります
----/////////////////////ここからユーザー設定
set RakutenURL to "http://pt.afl.rakuten.co.jp/c/" as Unicode text
set RakutenAFID to "XXXXXXXXXXXXXX" as Unicode text
----/////////////////////ここまでユーザー設定
set OriginalURL to "" as Unicode text
--クラス指定等無い場合は空欄のまま
set LinkClass to "" as Unicode text
set relTagd to "" as Unicode text
--CSSクラス指定等有る場合はこちらの記述
--set LinClass to " class=\"TextLink\"" as Unicode text
--set relTagd to " rel=\"nofollow\"" as Unicode text
---リンクテキストを固定の場合はこちらの記述
set LinkMes to "[LINK]" as Unicode text
---リンク先のドキュメントのタイトルをリンクテキストにする場合はこちら
---set LinkMes to "" as Unicode text
set LINK_TEXT_A to "を現在のウィンドで開きます" as Unicode text
set LINK_TEXT_B to "を新しいウィンドで開きます" as Unicode text
tell application "Finder"
activate
---ダイアログを出して
display dialog "リンクを作りたいURLをペーストしてください" default answer the OriginalURL buttons {"リンクを作成する", " 終了 "} with icon 1 with title "URLをペーストしてください" default button 1
if button returned of the result is "リンクを作成する" then
copy the result as list to {the OriginalURL, BottPressNo}
set ReplaceText to the OriginalURL as Unicode text
----URLエンコード
set encodeURLText to my encodeURL(ReplaceText) as Unicode text
set encodeURLTextZ to the encodeURLText as Unicode text
else if button returned of the result is "終了" then
end if
end tell
---よく使われる文字だけURLエンコードから戻す
set encodeURLText to replace(the encodeURLText, "%2F", "/") as text
set encodeURLText to replace(the encodeURLText, "%3A", ":") as text
set encodeURLText to replace(the encodeURLText, "%3F", "?") as text
set encodeURLText to replace(the encodeURLText, "%3D", "=") as text
set encodeURLText to replace(the encodeURLText, "%26", "&") as text
set encodeURLText to replace(the encodeURLText, "%23", "#") as text
---リンク先のドキュメントのタイトルを取得する
tell application "Safari"
launch
activate
make new document
delay 1
tell window 1
open location encodeURLText
end tell
delay 4
set theTitle to (name of document 1) as Unicode text
end tell
tell application "Safari"
close window 1
end tell
----致命的なタイトル名が来た場合の保険
set theTitle to replace(the theTitle, ">", ">") as text
set theTitle to replace(the theTitle, "<", "<") as text
set UrlTitle to theTitle as Unicode text
tell application "Finder"
set URLText to encodeURLText as Unicode text
set LinkTitle to UrlTitle as Unicode text
set LINK_TAG_A to "<a href=\"" & RakutenURL & RakutenAFID & "/?url=" & encodeURLTextZ & "&scid=af_ich_link_urltxt_pc\" " & LinkClass & relTagd & " title=\"" & UrlTitle & LINK_TEXT_A & "\">" as Unicode text
set LINK_TAG_B to "<a href=\"" & RakutenURL & RakutenAFID & "/?url=" & encodeURLTextZ & "&scid=af_ich_link_urltxt_pc\" " & LinkClass & relTagd & " title=\"" & UrlTitle & LINK_TEXT_B & "\">" as Unicode text
set LINK_IMAGES to "<img src=\"http://force4uip.appspot.com/images/10x10_nw.png\" alt=\"アイコン:新しいウィンドで開きます\" width=\"10\" height=\"10\" hspace=\"5\" /></a>" as Unicode text
set AccessibilityLink to LINK_TAG_A & LinkTitle & "</a>" & LINK_TAG_B & LINK_IMAGES
activate
----ダイアログに表示
display dialog "リンク用のタグが出来ました\nコピーして使って下さい。" default answer the AccessibilityLink with icon 1 with title "URLをコピーしてください" default button 1
end tell
-----URLエンコードのサブルーチン
on encodeURL(str)
set scpt to "php -r 'echo urlencode(\"" & str & "\");'"
return do shell script scpt
end encodeURL
-----文字の置換えサブルーチン(「Web Scripter's Meeting」より参照)
on replace(src, tg, rp)
set oldDel to AppleScript's text item delimiters
set AppleScript's text item delimiters to tg
set myList to text items of src
set AppleScript's text item delimiters to rp
set myText to myList as string
set AppleScript's text item delimiters to oldDel
return myText
end replace
| 固定リンク
「AppleScript」カテゴリの記事
- 濁音 半濁音 置換用レコード(2023.08.21)
- [AppleScript]キーノートの書類サイズを指定して作成(2022.01.09)
- [awk]行頭のスペースを削除する(subで置き換え)(2021.11.16)
- [SpotLight]選択範囲でSpotLight検索(2021.11.03)
- [AppleScript]リソースフォーク(カスタムアイコン)削除(2021.10.12)
この記事へのコメントは終了しました。


[RSS]
コメント