Encode %

[AppleScript] パーセント %エンコード


AppleScript サンプルコード

【スクリプトエディタで開く】 |

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004#
005#com.cocolog-nifty.quicktimer.icefloe
006----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
007use AppleScript version "2.8"
008use framework "Foundation"
009use framework "AppKit"
010use scripting additions
011property refMe : a reference to current application
012
013
014set strText to ("🇯🇵") as text
015
016
017set ocidTextString to refMe's NSString's stringWithString:(strText)
018
019
020##【1】AppleScript 大文字 %エンコード
021if strText starts with "http" then
022  ##URLの場合
023  #webURL
024  set ocidURL to refMe's NSURL's alloc()'s initWithString:(ocidTextString)
025  set strTextToEncoded to ocidURL's absoluteString() as text
026else if strText starts with "file:/" then
027  #ファイルURLの場合
028  set ocidFilePathURL to (refMe's NSURL's alloc()'s initWithString:(ocidTextString))
029  set strTextToEncoded to ocidFilePathURL's absoluteString() as text
030else
031  #通常文字列
032  ##キャラクタセットを指定
033  set ocidChrSet to (refMe's NSCharacterSet's URLQueryAllowedCharacterSet)
034  ##キャラクタセットで変換
035  set ocidTextToEncoded to ocidTextString's stringByAddingPercentEncodingWithAllowedCharacters:(ocidChrSet)
036  ##テキスト形式に確定
037  set strTextToEncoded to ocidTextToEncoded as text
038end if
039
040log strTextToEncoded as text
041
042
043##【1】AppleScript 小文字 %エンコード
044if strText starts with "http" then
045  ##URLの場合
046  #webURL
047  set ocidURL to refMe's NSURL's alloc()'s initWithString:(ocidTextString)
048  set strTextToEncoded to ocidURL's absoluteString() as text
049else if strText starts with "file:/" then
050  #ファイルURLの場合
051  set ocidFilePathURL to (refMe's NSURL's alloc()'s initWithString:(ocidTextString))
052  set strTextToEncoded to ocidFilePathURL's absoluteString() as text
053else
054  #通常文字列
055  ##キャラクタセットを指定
056  set ocidChrSet to (refMe's NSCharacterSet's URLQueryAllowedCharacterSet)
057  ##キャラクタセットで変換
058  set ocidTextToEncoded to ocidTextString's stringByAddingPercentEncodingWithAllowedCharacters:(ocidChrSet)
059  set ocidTextToEncoded to ocidTextToEncoded's lowercaseString()
060  ##テキスト形式に確定
061  set strTextToEncoded to ocidTextToEncoded as text
062end if
063
064log strTextToEncoded as text
AppleScriptで生成しました

|

[zsh]パーセント %エンコード


AppleScript サンプルコード

【スクリプトエディタで開く】 |

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004#
005#com.cocolog-nifty.quicktimer.icefloe
006----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
007use AppleScript version "2.8"
008use framework "Foundation"
009use scripting additions
010property refMe : a reference to current application
011
012
013set strText to ("🇯🇵") as text
014
015
016
017##【1】コマンド 小文字 %エンコード
018
019set strCommandText to ("/usr/bin/printf \"" & strText & "\" | /usr/bin/xxd -p| /usr/bin/sed 's/../%&/g'") as text
020log "\n" & strCommandText & "\n"
021#コマンド実行
022set strExecCommand to ("/bin/zsh -c \"" & strCommandText & "\"") as text
023try
024  set strResponse to (do shell script strExecCommand) as text
025on error
026  log "osascript でエラーしました"
027  return false
028end try
029
030
031##【2】コマンド 大文字 %エンコード
032
033set strCommandText to ("/usr/bin/printf \"" & strText & "\" | /usr/bin/xxd -p | /usr/bin/tr '[:lower:]' '[:upper:]' | /usr/bin/sed 's/../%&/g'") as text
034log "\n" & strCommandText & "\n"
035#コマンド実行
036set strExecCommand to ("/bin/zsh -c \"" & strCommandText & "\"") as text
037try
038  set strResponse to (do shell script strExecCommand) as text
039on error
040  log "osascript でエラーしました"
041  return false
042end try
AppleScriptで生成しました

|

[python] パーセント %エンコード


AppleScript サンプルコード

【スクリプトエディタで開く】 |

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004#
005#com.cocolog-nifty.quicktimer.icefloe
006----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
007use AppleScript version "2.8"
008use framework "Foundation"
009use scripting additions
010property refMe : a reference to current application
011
012
013set strText to ("🇯🇵") as text
014
015
016##【1】python3コマンド 大文字 %エンコード
017
018set strCommandText to ("dectext=\"" & strText & "\"; print(\"&\" + \"&\".join(f\"{byte:02X}\" for byte in dectext.encode()))") as «class utf8»
019log "\n" & strCommandText & "\n"
020#コマンド実行
021set strExecCommand to ("/usr/bin/python3 -c '" & strCommandText & "'") as text
022try
023  set strResponse to (do shell script strExecCommand) as text
024on error
025  log "osascript でエラーしました"
026  return false
027end try
028
029
030##【2】python3コマンド 小文字 %エンコード
031
032set strCommandText to ("dectext=\"" & strText & "\"; print(\"&\" + \"&\".join(f\"{byte:02x}\" for byte in dectext.encode()))") as «class utf8»
033log "\n" & strCommandText & "\n"
034#コマンド実行
035set strExecCommand to ("/usr/bin/python3 -c '" & strCommandText & "'") as text
036try
037  set strResponse to (do shell script strExecCommand) as text
038on error
039  log "osascript でエラーしました"
040  return false
041end try
AppleScriptで生成しました

|

URLエンコード(いわゆる%エンコード)修正


AppleScript サンプルコード

【スクリプトエディタで開く】 |

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004#
005#
006#com.cocolog-nifty.quicktimer.icefloe
007----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
008use AppleScript version "2.8"
009use framework "Foundation"
010use framework "AppKit"
011use scripting additions
012
013property refMe : a reference to current application
014
015################################
016##デフォルトクリップボードからテキスト取得
017################################
018set appPasteboard to refMe's NSPasteboard's generalPasteboard()
019set ocidTypeArray to appPasteboard's types()
020set boolContain to ocidTypeArray's containsObject:("public.utf8-plain-text")
021if boolContain = true then
022  try
023    set ocidPasteboardArray to appPasteboard's readObjectsForClasses:({refMe's NSString}) options:(missing value)
024    set ocidPasteboardStrings to ocidPasteboardArray's firstObject()
025  on error
026    set ocidStringData to appPasteboard's stringForType:("public.utf8-plain-text")
027    set ocidPasteboardStrings to (refMe's NSString's stringWithString:(ocidStringData))
028  end try
029else
030  set ocidPasteboardStrings to (refMe's NSString's stringWithString:(""))
031end if
032set strDefaultAnswer to ocidPasteboardStrings as text
033
034################################
035######ダイアログ
036################################
037#####ダイアログを前面に
038tell current application
039  set strName to name as text
040end tell
041####スクリプトメニューから実行したら
042if strName is "osascript" then
043  tell application "Finder" to activate
044else
045  tell current application to activate
046end if
047set aliasIconPath to (POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/BookmarkIcon.icns") as alias
048try
049  set strMes to ("読める文字を%エンコードします\nテキストやURL等を入力してください") as text
050  
051  set recordResponse to (display dialog strMes with title "入力してください" default answer strDefaultAnswer buttons {"OK", "キャンセル"} default button "OK" cancel button "キャンセル" with icon aliasIconPath giving up after 20 without hidden answer)
052on error
053  log "エラーしました"
054  return "エラーしました"
055end try
056if true is equal to (gave up of recordResponse) then
057  return "時間切れですやりなおしてください"
058end if
059if "OK" is equal to (button returned of recordResponse) then
060  set strResponse to (text returned of recordResponse) as text
061else
062  log "キャンセルしました"
063  return "キャンセルしました"
064end if
065
066##############################
067###URLと通常テキストの処理を分岐する
068##URLの場合
069set ocidTextString to refMe's NSString's stringWithString:(strResponse)
070if strResponse starts with "http" then
071  #webURL
072  set ocidURL to refMe's NSURL's alloc()'s initWithString:(ocidTextString)
073  set strTextToEncoded to ocidURL's absoluteString() as text
074else if strResponse starts with "file:/" then
075  #FILEURL
076  set ocidFilePathURL to (refMe's NSURL's alloc()'s initWithString:(ocidTextString))
077  set strTextToEncoded to ocidFilePathURL's absoluteString() as text
078else
079  #通常文字列
080  ##キャラクタセットを指定
081  set ocidChrSet to (refMe's NSCharacterSet's URLQueryAllowedCharacterSet)
082  ##キャラクタセットで変換
083  set ocidTextToEncoded to ocidTextString's stringByAddingPercentEncodingWithAllowedCharacters:(ocidChrSet)
084  ##テキスト形式に確定
085  set strTextToEncoded to ocidTextToEncoded as text
086end if
087
088
089################################
090######ダイアログ
091################################
092#####ダイアログを前面に
093tell current application
094  set strName to name as text
095end tell
096####スクリプトメニューから実行したら
097if strName is "osascript" then
098  tell application "Finder" to activate
099else
100  tell current application to activate
101end if
102set aliasIconPath to POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertNoteIcon.icns"
103set strMes to ("戻り値です\r" & strTextToEncoded) as text
104
105set recordResult to (display dialog strMes with title "%エンコード結果" default answer strTextToEncoded buttons {"クリップボードにコピー", "キャンセル", "OK"} default button "OK" cancel button "キャンセル" giving up after 20 with icon aliasIconPath without hidden answer)
106
107if button returned of recordResult is "クリップボードにコピー" then
108  try
109    set strText to text returned of recordResult as text
110    ####ペーストボード宣言
111    set appPasteboard to refMe's NSPasteboard's generalPasteboard()
112    set ocidText to (refMe's NSString's stringWithString:(strText))
113    appPasteboard's clearContents()
114    appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
115  on error
116    tell application "Finder"
117      set the clipboard to strText as text
118    end tell
119  end try
120end if
121
122return
AppleScriptで生成しました

|

%エンコードされたURLのデコード


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application

set strURL to "https://www.foo.com/cgi-lib/%E6%97%A5%E6%9C%AC%E8%AA%9E%E3%81%AE%E3%83%91%E3%82%B9/url.cgi?AAAA=%E6%97%A5%E6%9C%AC%E8%AA%9E%E3%81%AE%E3%82%AF%E3%82%A8%E3%83%AA%E3%83%BC"

set ocidURLstr to refMe's NSString's stringWithString:(strURL)
set ocidURL to ocidURLstr's stringByRemovingPercentEncoding
log ocidURL as text
-->
(*https://www.foo.com/cgi-lib/日本語のパス/url.cgi?AAAA=日本語のクエリー*)
return


|

URLの%エンコード


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application

set strURL to "https://www.foo.com/cgi-lib/日本語のパス/url.cgi?AAAA=日本語のクエリー"

set ocidURLstr to refMe's NSString's stringWithString:(strURL)
set ocidBaseURL to refMe's NSURL's alloc()'s initWithString:(ocidURLstr)
set ocidURLomponents to refMe's NSURLComponents's componentsWithURL:(ocidBaseURL) resolvingAgainstBaseURL:(false)
set ocidURL to ocidURLomponents's |URL|
set strURL to ocidURL's absoluteString() as text
log strURL as text

-->
(*https://www.foo.com/cgi-lib/encode/url.cgi?AAAA=%E6%97%A5%E6%9C%AC%E8%AA%9E%E3%81%AE%E3%82%AF%E3%82%A8%E3%83%AA%E3%83%BC*)

return

|

[PercentEncode]URLエンコード


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions

property refMe : a reference to current application

################################
##デフォルトクリップボードからテキスト取得
################################
set appPasteboard to refMe's NSPasteboard's generalPasteboard()
set ocidTypeArray to appPasteboard's types()
set boolContain to ocidTypeArray's containsObject:("public.utf8-plain-text")
if boolContain = true then
  try
    set ocidPasteboardArray to appPasteboard's readObjectsForClasses:({refMe's NSString}) options:(missing value)
    set ocidPasteboardStrings to ocidPasteboardArray's firstObject()
  on error
    set ocidStringData to appPasteboard's stringForType:("public.utf8-plain-text")
    set ocidPasteboardStrings to (refMe's NSString's stringWithString:(ocidStringData))
  end try
else
  set ocidPasteboardStrings to (refMe's NSString's stringWithString:(""))
end if
set strDefaultAnswer to ocidPasteboardStrings as text

################################
######ダイアログ
################################
#####ダイアログを前面に
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if
set aliasIconPath to (POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/BookmarkIcon.icns") as alias
try
  set strMes to ("読める文字を%エンコードします\nテキストやURL等を入力してください") as text
  
  set recordResponse to (display dialog strMes with title "入力してください" default answer strDefaultAnswer buttons {"OK", "キャンセル"} default button "OK" cancel button "キャンセル" with icon aliasIconPath giving up after 20 without hidden answer)
on error
  log "エラーしました"
return "エラーしました"
end try
if true is equal to (gave up of recordResponse) then
return "時間切れですやりなおしてください"
end if
if "OK" is equal to (button returned of recordResponse) then
  set strResponse to (text returned of recordResponse) as text
else
  log "キャンセルしました"
return "キャンセルしました"
end if

##############################
###URLと通常テキストの処理を分岐する
##URLの場合
if strResponse starts with "http" then
  ###タブと改行を除去しておく
  set ocidResponseText to refMe's NSString's stringWithString:(strResponse)
  set ocidTextM to refMe's NSMutableString's alloc()'s initWithCapacity:(0)
ocidTextM's appendString:(ocidResponseText)
  ##改行除去
  set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\n") withString:("")
  set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\r") withString:("")
  ##タブ除去
  set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\t") withString:("")
  set strURL to ocidTextM as text
  set strURL to doUrlDecode(strURL)
  ##テキスト
  set ocidArgText to refMe's NSString's stringWithString:(strURL)
  set ocidArgTextArray to ocidArgText's componentsSeparatedByString:("?")
  set numCntArray to (count of ocidArgTextArray) as integer
  if numCntArray > 1 then
    set ocidBaseURLstr to ocidArgTextArray's firstObject()
ocidArgTextArray's removeObjectAtIndex:(0)
    set ocidQueryStr to ocidArgTextArray's componentsJoinedByString:("?")
    log ocidQueryStr as text
    set ocidArgTextArray to ocidQueryStr's componentsSeparatedByString:("=")
    set numCntArray to (count of ocidArgTextArray) as integer
    if numCntArray > 1 then
      log ocidArgTextArray as list
      set strNewQuery to ((ocidArgTextArray's firstObject() as text) & "=") as text
      repeat with itemIntNo from 1 to (numCntArray - 2) by 1
set ocidItem to (ocidArgTextArray's objectAtIndex:(itemIntNo))
log ocidItem as text
set ocidItemArray to (ocidItem's componentsSeparatedByString:("&"))
set numCntArray to (count of ocidItemArray) as integer
if numCntArray > 1 then
set ocidNextQue to ocidItemArray's lastObject()
(ocidItemArray's removeLastObject())
set ocidItemQueryStr to (ocidItemArray's componentsJoinedByString:("&"))
set strEncValue to doUrlEncode(ocidItemQueryStr as text)
set ocidEncValue to (refMe's NSString's stringWithString:(strEncValue))
set ocidEncValue to (ocidEncValue's stringByReplacingOccurrencesOfString:("&") withString:("%26"))
set strNewQuery to strNewQuery & ((ocidEncValue as text) & "&" & (ocidNextQue as text) & "=") as text
else
set strNewQuery to strNewQuery & ((ocidItemArray's firstObject() as text) & "&" & (ocidItemArray's lastObject() as text) & "=") as text
end if
      end repeat
      set strNewQuery to (strNewQuery & (ocidArgTextArray's lastObject() as text)) as text
      log strNewQuery
      set strEncText to ((ocidBaseURLstr as text) & "?" & strNewQuery) as text
    end if
    ##URLのクエリーにNameが無い場合
    set strEncText to ((ocidBaseURLstr as text) & "?" & (ocidQueryStr as text)) as text
    set strURL to doUrlDecode(strEncText)
    set strEncText to doUrlEncode(strURL)
  else
    ##URLに?がない=クエリーがない場合
    set ocidBaseURLstr to ocidArgTextArray's firstObject()
    set strURL to doUrlDecode(ocidBaseURLstr)
    set strEncText to doUrlEncode(strURL)
  end if
else
  ##通常テキストの場合
  set strText to strResponse as text
  set strText to doUrlDecode(strText) as text
  set strEncText to doTextEncode(strText) as text
end if
log strEncText
################################
######ダイアログ
################################
#####ダイアログを前面に
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if
set aliasIconPath to POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertNoteIcon.icns"
set strMes to ("戻り値です\r" & strEncText) as text

set recordResult to (display dialog strMes with title "%エンコード結果" default answer strEncText buttons {"クリップボードにコピー", "キャンセル", "OK"} default button "OK" cancel button "キャンセル" giving up after 20 with icon aliasIconPath without hidden answer)

if button returned of recordResult is "クリップボードにコピー" then
  try
    set strText to text returned of recordResult as text
    ####ペーストボード宣言
    set appPasteboard to refMe's NSPasteboard's generalPasteboard()
    set ocidText to (refMe's NSString's stringWithString:(strEncText))
appPasteboard's clearContents()
appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
  on error
    tell application "Finder"
      set the clipboard to strEncText as text
    end tell
  end try
end if






####################################
###### %デコード
####################################
on doUrlDecode(argText)
  ##テキスト
  set ocidArgText to refMe's NSString's stringWithString:(argText)
  ##デコード
  set ocidArgTextEncoded to ocidArgText's stringByRemovingPercentEncoding
  set strArgTextEncoded to ocidArgTextEncoded as text
return strArgTextEncoded
end doUrlDecode
####################################
###### %エンコード
####################################
on doUrlEncode(argText)
  ##テキスト
  set ocidArgText to refMe's NSString's stringWithString:(argText)
  ##キャラクタセットを指定
  set ocidChrSet to refMe's NSCharacterSet's URLQueryAllowedCharacterSet
  ##キャラクタセットで変換
  set ocidArgTextEncoded to ocidArgText's stringByAddingPercentEncodingWithAllowedCharacters:(ocidChrSet)
  ##テキスト形式に確定
  set strTextToEncode to ocidArgTextEncoded as text
  ###値を戻す
return strTextToEncode
end doUrlEncode
####################################
###### %エンコード
####################################
on doTextEncode(argText)
  ##テキスト
  set ocidArgText to refMe's NSString's stringWithString:(argText)
  ##キャラクタセットを指定
  set ocidChrSet to refMe's NSCharacterSet's URLQueryAllowedCharacterSet
  ##キャラクタセットで変換
  set ocidArgTextEncoded to ocidArgText's stringByAddingPercentEncodingWithAllowedCharacters:(ocidChrSet)
  ######## 置換 %エンコードの追加処理
  ###置換レコード
  set recordPercentMap to {|!|:"%21", |#|:"%23", |$|:"%24", |&|:"%26", |'|:"%27", |(|:"%28", |)|:"%29", |*|:"%2A", |+|:"%2B", |,|:"%2C", |:|:"%3A", |;|:"%3B", |=|:"%3D", |?|:"%3F", |@|:"%40", | |:"%20"} as record
  ###ディクショナリにして
  set ocidPercentMap to refMe's NSDictionary's alloc()'s initWithDictionary:(recordPercentMap)
  ###キーの一覧を取り出します
  set ocidAllKeys to ocidPercentMap's allKeys()
  ###取り出したキー一覧を順番に処理
  repeat with itemAllKey in ocidAllKeys
    set strItemKey to itemAllKey as text
    ##キーの値を取り出して
    if strItemKey is "@" then
      ##置換
      set ocidEncodedText to (ocidArgTextEncoded's stringByReplacingOccurrencesOfString:("@") withString:("%40"))
    else
      set ocidMapValue to (ocidPercentMap's valueForKey:(strItemKey))
      ##置換
      set ocidEncodedText to (ocidArgTextEncoded's stringByReplacingOccurrencesOfString:(strItemKey) withString:(ocidMapValue))
    end if
    
    ##次の変換に備える
    set ocidArgTextEncoded to ocidEncodedText
  end repeat
  ##テキスト形式に確定
  set strTextToEncode to ocidEncodedText as text
  ###値を戻す
return strTextToEncode
end doTextEncode



|

[PercentEncode]URLエンコード・デコード


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions

property refMe : a reference to current application

################################
##デフォルトクリップボードからテキスト取得
################################
set appPasteboard to refMe's NSPasteboard's generalPasteboard()
set ocidTypeArray to appPasteboard's types()
set boolContain to ocidTypeArray's containsObject:("public.utf8-plain-text")
if boolContain = true then
  try
    set ocidPasteboardArray to appPasteboard's readObjectsForClasses:({refMe's NSString}) options:(missing value)
    set ocidPasteboardStrings to ocidPasteboardArray's firstObject()
  on error
    set ocidStringData to appPasteboard's stringForType:("public.utf8-plain-text")
    set ocidPasteboardStrings to (refMe's NSString's stringWithString:(ocidStringData))
  end try
else
  set ocidPasteboardStrings to (refMe's NSString's stringWithString:(""))
end if
set strDefaultAnswer to ocidPasteboardStrings as text

################################
######ダイアログ
################################
#####ダイアログを前面に
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if
set aliasIconPath to (POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/BookmarkIcon.icns") as alias
try
  set strMes to ("読める文字を%エンコードします\nテキストやURL等を入力してください") as text
  
  set recordResponse to (display dialog strMes with title "入力してください" default answer strDefaultAnswer buttons {"OK", "キャンセル"} default button "OK" cancel button "キャンセル" with icon aliasIconPath giving up after 20 without hidden answer)
on error
  log "エラーしました"
return "エラーしました"
end try
if true is equal to (gave up of recordResponse) then
return "時間切れですやりなおしてください"
end if
if "OK" is equal to (button returned of recordResponse) then
  set strResponse to (text returned of recordResponse) as text
else
  log "キャンセルしました"
return "キャンセルしました"
end if

##通常テキストの場合
set strText to strResponse as text
set strDecodedText to doUrlDecode(strText) as text

################################
######ダイアログ
################################
#####ダイアログを前面に
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if
set aliasIconPath to POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertNoteIcon.icns"
set strMes to ("戻り値です\r" & strDecodedText) as text

set recordResult to (display dialog strMes with title "%デコード" default answer strDecodedText buttons {"クリップボードにコピー", "キャンセル", "OK"} default button "OK" cancel button "キャンセル" giving up after 20 with icon aliasIconPath without hidden answer)

if button returned of recordResult is "クリップボードにコピー" then
  try
    set strText to text returned of recordResult as text
    ####ペーストボード宣言
    set appPasteboard to refMe's NSPasteboard's generalPasteboard()
    set ocidText to (refMe's NSString's stringWithString:(strText))
appPasteboard's clearContents()
appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
  on error
    tell application "Finder"
      set the clipboard to strText as text
    end tell
  end try
end if






####################################
###### %デコード
####################################
on doUrlDecode(argText)
  ##テキスト
  set ocidArgText to refMe's NSString's stringWithString:(argText)
  ##デコード
  set ocidArgTextEncoded to ocidArgText's stringByRemovingPercentEncoding
  set strArgTextEncoded to ocidArgTextEncoded as text
return strArgTextEncoded
end doUrlDecode
####################################
###### %エンコード
####################################
on doUrlEncode(argText)
  ##テキスト
  set ocidArgText to refMe's NSString's stringWithString:(argText)
  ##キャラクタセットを指定
  set ocidChrSet to refMe's NSCharacterSet's URLQueryAllowedCharacterSet
  ##キャラクタセットで変換
  set ocidArgTextEncoded to ocidArgText's stringByAddingPercentEncodingWithAllowedCharacters:(ocidChrSet)
  ##テキスト形式に確定
  set strTextToEncode to ocidArgTextEncoded as text
  ###値を戻す
return strTextToEncode
end doUrlEncode
####################################
###### %エンコード
####################################
on doTextEncode(argText)
  ##テキスト
  set ocidArgText to refMe's NSString's stringWithString:(argText)
  ##キャラクタセットを指定
  set ocidChrSet to refMe's NSCharacterSet's URLQueryAllowedCharacterSet
  ##キャラクタセットで変換
  set ocidArgTextEncoded to ocidArgText's stringByAddingPercentEncodingWithAllowedCharacters:(ocidChrSet)
  ######## 置換 %エンコードの追加処理
  ###置換レコード
  set recordPercentMap to {|!|:"%21", |#|:"%23", |$|:"%24", |&|:"%26", |'|:"%27", |(|:"%28", |)|:"%29", |*|:"%2A", |+|:"%2B", |,|:"%2C", |:|:"%3A", |;|:"%3B", |=|:"%3D", |?|:"%3F", |@|:"%40", | |:"%20"} as record
  ###ディクショナリにして
  set ocidPercentMap to refMe's NSDictionary's alloc()'s initWithDictionary:(recordPercentMap)
  ###キーの一覧を取り出します
  set ocidAllKeys to ocidPercentMap's allKeys()
  ###取り出したキー一覧を順番に処理
  repeat with itemAllKey in ocidAllKeys
    set strItemKey to itemAllKey as text
    ##キーの値を取り出して
    if strItemKey is "@" then
      ##置換
      set ocidEncodedText to (ocidArgTextEncoded's stringByReplacingOccurrencesOfString:("@") withString:("%40"))
    else
      set ocidMapValue to (ocidPercentMap's valueForKey:(strItemKey))
      ##置換
      set ocidEncodedText to (ocidArgTextEncoded's stringByReplacingOccurrencesOfString:(strItemKey) withString:(ocidMapValue))
    end if
    
    ##次の変換に備える
    set ocidArgTextEncoded to ocidEncodedText
  end repeat
  ##テキスト形式に確定
  set strTextToEncode to ocidEncodedText as text
  ###値を戻す
return strTextToEncode
end doTextEncode



|

[%エンコード]NSCharacterSetの指定で%エンコード

#!/usr/bin/env osascript

----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

#

#

#

#

#                       com.cocolog-nifty.quicktimer.icefloe

----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

##自分環境がos12なので2.8にしているだけです

use AppleScript version "2.8"

use framework "Foundation"

use scripting additions


property refMe : a reference to current application

property refNSString : a reference to refMe's NSString

property refNSURL : a reference to refMe's NSURL



###

set strFilePathText to "~/Desktop/名称未設定フォルダ"

###String

set ocidFilePath to (refNSString's stringWithString:strFilePathText)

###絶対パスで

set ocidFilePathString to ocidFilePath's stringByStandardizingPath

###NSURL

set ocidFilePathURL to (refNSURL's alloc()'s initFileURLWithPath:ocidFilePathString isDirectory:true)

###NSCFString

set ocidRFCurl to ocidFilePathURL's absoluteString()

log ocidRFCurl as text

log className() of ocidRFCurl as text

###デーコード済みパス

set ocidDecodeURL to ocidRFCurl's stringByRemovingPercentEncoding()

log ocidDecodeURL as text

log className() of ocidDecodeURL as text


###エンコード ファイルパスの場合

##ファイルパス用のキャラクターセットを定義

set ocidURLPathAllowedCharacterSet to refMe's NSCharacterSet's URLPathAllowedCharacterSet

###エンコード

set ocidEcodeURL to ocidDecodeURL's stringByAddingPercentEncodingWithAllowedCharacters:(ocidURLPathAllowedCharacterSet)

log ocidEcodeURL as text


####エンコード URLクエリーの場合

####URLクエリー用のキャラクターセットを定義

set ocidURLQueryAllowedCharacterSet to refMe's NSCharacterSet's URLQueryAllowedCharacterSet

###エンコード

set ocidEcodeURL to ocidDecodeURL's stringByAddingPercentEncodingWithAllowedCharacters:(ocidURLQueryAllowedCharacterSet)

log ocidEcodeURL as text





|

[URL encode] NSString %エンコードされた文字列を読める文字にデコードする

URLエンコード %エンコード

ダウンロード - urlencode.zip


#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

use AppleScript version "2.6"
use framework "Foundation"
use scripting additions

property objMe : a reference to current application
property objNSString : a reference to objMe's NSString
property objNSStringNSURL : a reference to objMe's NSURL


set strFileName to "%E7%BE%8E%E3%81%97%E3%81%84%E6%97%A5%E6%9C%AC%E8%AA%9E%E3%81%AE%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E5%90%8D" as text

set strEncod to urlDecode(strFileName) as text



on urlDecode(input)
tell objNSString to set rawUrl to stringWithString_(input)
set theEncodedURL to rawUrl's stringByRemovingPercentEncoding
return theEncodedURL as text
end urlDecode

|

その他のカテゴリー

Accessibility Acrobat Acrobat 2020 Acrobat AddOn Acrobat Annotation Acrobat ARMDC Acrobat AV2 Acrobat BookMark Acrobat Classic Acrobat DC Acrobat Dialog Acrobat Distiller Acrobat Form Acrobat JS Acrobat Manifest Acrobat Menu Acrobat Open Acrobat Plugin Acrobat Preferences Acrobat Preflight Acrobat python Acrobat Reader Acrobat SCA Acrobat SCA Updater Acrobat Sequ Acrobat Sign Acrobat Stamps Acrobat Watermark Acrobat Windows Acrobat Windows Reader Admin Admin Account Admin Apachectl Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Mobileconfig Admin Permission Admin Pkg Admin Power Management Admin Printer Admin SetUp Admin SMB Admin Support Admin System Information Admin Tools Admin Users Admin Volumes Adobe Adobe FDKO Adobe RemoteUpdateManager AppKit Apple AppleScript AppleScript do shell script AppleScript List AppleScript ObjC AppleScript Osax AppleScript PDF AppleScript Pictures AppleScript record AppleScript Script Editor AppleScript Script Menu AppleScript Shortcuts AppleScript Shortcuts Events AppleScript System Events AppleScript System Events Plist AppleScript Video Applications AppStore Archive Attributes Automator BackUp Barcode Barcode QR Barcode QR Decode Bash Basic Basic Path Bluetooth BOX Browser Calendar CD/DVD Choose Chrome CIImage CityCode CloudStorage Color com.apple.LaunchServices.OpenWith Console Contacts CotEditor CURL current application Date&Time delimiters Desktop Device Diff Disk Dock DropBox Droplet eMail Encode % Encode Decode Encode UTF8 Error EXIFData ffmpeg File Finder Firefox Folder FolderAction Fonts GIF github Guide HTML HTML Entity Icon Illustrator Image Events Image2PDF ImageOptim iPhone iWork Javascript Jedit Json Label Leading Zero List locationd LRC lsappinfo LSSharedFileList m3u8 Mail MakePDF Map Math Media Media AVAsset Media AVconvert Media AVFoundation Media AVURLAsset Media Movie Media Music Memo Messages Microsoft Microsoft Edge Microsoft Excel Mouse Music NetWork Notes NSArray NSArray Sort NSBezierPath NSBitmapImageRep NSBundle NSCFBoolean NSCharacterSet NSColor NSColorList NSData NSDecimalNumber NSDictionary NSError NSEvent NSFileAttributes NSFileManager NSFileManager enumeratorAtURL NSFont NSFontManager NSGraphicsContext NSImage NSIndex NSKeyedArchiver NSKeyedUnarchiver NSLocale NSMutableArray NSMutableDictionary NSMutableString NSNotFound NSNumber NSOpenPanel NSPasteboard NSpoint NSPredicate NSPrintOperation NSRange NSRect NSRegularExpression NSRunningApplication NSScreen NSSize NSString NSString stringByApplyingTransform NSStringCompareOptions NSTask NSTimeZone NSURL NSURL File NSURLBookmark NSURLComponents NSURLResourceKey NSURLSession NSUserDefaults NSUUID NSView NSWorkspace Numbers OAuth OneDrive PDF PDFAnnotation PDFAnnotationWidget PDFContext PDFDisplayBox PDFDocumentPermissions PDFImageRep PDFKit PDFnUP PDFOutline perl Photoshop PlistBuddy pluginkit postalcode PostScript prefPane Preview Python QuickLook QuickTime ReadMe Regular Expression Reminders ReName Repeat RTF Safari SaveFile ScreenCapture ScreenSaver SF Symbols character id SF Symbols Entity sips Skype Slack Sound Spotlight sqlite SRT StandardAdditions Swift System Settings TCC TemporaryItems Terminal Text Text CSV Text MD Text TSV TextEdit Tools Translate Trash Twitter Typography UI Unit Conversion UTType valueForKeyPath Video VisionKit Visual Studio Code Wacom webarchive webp Wifi Windows XML XML EPUB XML OPML XML Plist XML RSS XML savedSearch XML SVG XML TTML XML webloc XML XMP YouTube zoom