coding()ãã³ãã©ã®ç価ã¨åºããä¸ç
æ¨æ¥å®è£ ããcoding()ãã³ãã©ã¯ãå°ã大è¢è£ãããããªããã©ãå®ã¯èªåã®ä¸ã§ã¯é·å¹´ã®å¤¢ã ã£ãã
on coding(obj) try
if obj's class = text then
"\"" & obj & "\""
else if obj's class = list or obj's class = record then
obj as number
else
obj as text
end if
on error msg
try
"require \"jcode\";$KCODE=\"u\";/\\{.*\\}/=~" & quoted form of msg & ";puts($&);" --rubyã³ã¼ã
do shell script "ruby -e " & quoted form of result --ã·ã§ã«ã³ã¼ã
on error
--do shell scriptã®262,144ãã¤ãå¶éã¨ã©ã¼ã§ããæ¥æ¬èªç°å¢ãªãæããã
--http://developer.apple.com/jp/technotes/tn2002/tn2065.html
msg's items 1 thru -24 as text --æ¥æ¬èªç°å¢ä»¥å¤ã§ã¯NG
end try
end try
end coding
RubyãPerlã®ããã·ã¥ãJavaScriptãPythonã»PHPã®é£æ³é
åãªã©å¤ãã®ã¹ã¯ãªããè¨èªã¯ããã®å¼ã³åã¯éããã©ãä»»æã®ãã¼ã§å¤ã«ã¢ã¯ã»ã¹å¯è½ãªé
åãåºæ¬æ©è½ã¨ãã¦åãã¦ããããã¼ã®æå®ã¯æååã§å¯è½ã§ãå¤æ°ã«ä»£å
¥ãããæååã§ãã¢ã¯ã»ã¹ã§ããããã¼ãå¤ã¸ã®ã¢ã¯ã»ã¹ã¯å¯è½ãªéãèªç±ããã®è¿½å ã»å¤æ´ã»åé¤ãèªç±ãããã·ã¥ãé£æ³é
åã«èªç±ã«ã¢ã¯ã»ã¹ã§ãã¦ããã使ããããè¨èªç°å¢ã¨ãããã
ã¾ããããã·ã¥ãé£æ³é
åã¯ãå¤ãã®è¨èªã§ã·ãªã¢ã©ã¤ãºï¼XMLããã¤ãã³ã¼ããªã©ã«å¤æãã¦ãã¼ã¿ã¨ãã¦ä¿åã§ãããã¨ï¼å¯è½ã ãããã¦ãå¤ãã®è¨èªãJSONã®èªã¿æ¸ãï¼ãã·ãªã¢ã©ã¤ãºã»ã·ãªã¢ã©ã¤ãºï¼ããµãã¼ããã¦ãããã¤ã¾ããJSONã使ãã°ãç°ãªãè¨èªéã§ããã¼ã¿ã®åã渡ããç°¡åã«è¡ããã®ã ããããªç°å¢ã«ãªã£ã¦ããã
AppleScriptãããããä¼¼ããããªæ©è½ã«ã¬ã³ã¼ãããããã¨ããããAppleScriptã®ã¬ã³ã¼ãã«ã¯èªç±ããªã...ã
- ä»»æã®ãã¼ã§ã¢ã¯ã»ã¹ã§ããã®ã ãããã®ãã¼ã¯æååã§ã¯ãªããï¼ã³ãã³ã§åºåãããå·¦å´ã®ãã¼ã³ã¼ãã¨ãã¦ãã³ã¼ãä¸ã§è§£æããã¦ããã¼ã¨ãã¦èªèãããå¿ è¦ããããã ããå¤æ°ã«ä»£å ¥ããæååã¯ãç°¡åã«ã¯ãã¼ã¨ãã¦ä½¿ããªãã
- ããããä¸æ¦ç»é²ããã¬ã³ã¼ãã®ãã¼ã¯ãå¤æ´ã¯ã§ããããåé¤ããæ¹æ³ããªã...ã
- ã¾ããã¬ã³ã¼ãã¯ãã¡ã¤ã«ã«ä¿åã§ããããããã¯AppleScriptç¬èªã®å½¢å¼ã§ããã人éãè¦ã¦ãæå³ä¸æãä»ã®è¨èªãAppleScriptããµãã¼ããã訳ããªããAppleScriptã®ã¬ã³ã¼ããä»ã®è¨èªã«æ¸¡ãã®ã¯é常ã«é¢åã ã£ãã
- ä¸æ¹ã§AppleScriptã¯ãä»ã®è¨èªããã®ãã¼ã¿ã¯ä¸æã«åãåããdo shell script ã Safariçµç±ã®do JavaScriptã¯ããã¡ãã®ä¸çã®å¦ççµæãæ°å¤ã»æååã»ãªã¹ãã»ã¬ã³ã¼ããªã©æé©ãªå½¢å¼ã§è¿ã£ã¦ããã
ã¤ã¾ããæ¥ããã®æã¾ããåºããã®åºããããããã¯ã人ã®ãã®ããªã¬ã®ã¢ãããªã¬ã®ã¢ãããªã¬ã®ã¢ãï¼ã¸ã£ã¤ã¢ã³ï¼ããããªç¶æ ã
- AppleScriptããä»ã®è¨èªç°å¢ã«ãã¼ã¿ã渡ãã«ã¯ãdo shell script ã do JavaScriptã§ã³ã¼ãçæããããæ¹æ³ããªãã
- ãªã®ã«JSONãããµãã¼ãããã¦ããããAppleScriptç¬èªã®ã¬ã³ã¼ãå½¢å¼ã§ããæ¸ãåºããªããï¼æå³ãªãï¼
- ããããAppleScriptã®ã¬ã³ã¼ãã使ãããããã°ãããããä»ã®è¨èªã«å¦çãä¾é ¼ããå¿ è¦æ§ã¯ãªãã®ã ã...ã
- ç´ ã®AppleScriptã§ã¯ããªã¹ãã»ã¬ã³ã¼ããæä½ããæ©è½ã貧弱éããã®ã ã
ä¸ä¾¿ãè£ã工夫ï¼å¤æ°ã®å¤ããã¼ã«ããã¬ã³ã¼ãå¤ã¸ã®ã¢ã¯ã»ã¹
- ã¬ã³ã¼ãã«æ¬ ãã¦ããæãéè¦ãªæ©è½ã ã
- ä½ã¯ã¨ããããã¹ã¯ãªããè¨èªã®åºæ¬ã¨è¨ãããã®æ©è½ã¯çµ¶å¯¾ã«æ¬²ããã
- ããããªãã¨ãä½ãããã«ãã¦ãã¹ã¼ãã¼ã¦ã«ãã©åé·ã«ãªã£ã¦ãã¾ãã
- ã§ãä»ã¾ã§ã©ããã¦ããããã¨ããã¨...Run Scriptã使ã£ã¦ããã
- Run Scriptã¯ãããã«ç¶ãæååãAppleScriptã¨ãã¦è§£éãã¦ãå®è¡ãã¦ãçµæãè¿ãã
- ãªã®ã§ä»¥ä¸ã®ããã«ããã¨ãa_keyã«ä»£å ¥ãã"b"ãå©ç¨ãã¦ã¬ã³ã¼ãã«ã¢ã¯ã»ã¹ã§ããã®ã ã
set a_key to "b"
run script "{a:1,b:2,c:3}'s" & a_key
- ã¨ããã§ãä¸è¨ã®ã¾ã¾ã§ã¯ {a:1,b:2,c:3} åºå®ã®ã¬ã³ã¼ãããæ±ããªãã
- coding()ãã³ãã©ãæã«å ¥ããä»ãªãç°¡åã«ã¬ã³ã¼ããã³ã¼ãåã§ãããã以åã¯ä»¥ä¸ã®ãããªãã³ãã©ãä½ã£ã¦åãã§ããã
on for_key2(a_record, a_key) try
((path to temporary items) as text) & "__value_for_key_record__.tmp"
set f to open for access file (result) with write permission
write a_record to f
end try
close access f
set res to run script "(read file (((path to temporary items) as text) & \"__value_for_key_record__.tmp\") as record)'s |" & a_key & "|"
--do shell script "rm ${TMPDIR}TemporaryItems/__value_for_key_record__.tmp" --åé¤ãæå¹ã«ããã¨é ãï¼2åãããï¼
res
end for_key2
- ã¬ã³ã¼ãã®å 容ãRun Scriptã®ä¸çã«æ¸¡ãããã
- ä¸æ¦ãã¡ã¤ã«ã¨ãã¦ä¿åã
- ãããRun Scriptã§ã¬ã³ã¼ãã¨ãã¦èªã¿åºãã¦ããã
- æ¶ãå¿ãã¦ãåèµ·åã§åé¤ãããä¸æãã©ã«ããå©ç¨ãããã
- ãã¹ã¯ã¼ããªã©ãéè¦ãªãã¼ã¿ãæ±ãæã¯ç´ãã«åé¤ããåªåããããã
- ä»æãã°ãç¸å½æ¶ãã¾ããåªåãããªããããã¼å¤ã¢ã¯ã»ã¹ããã¦ããã
coding()ãã³ãã©ä»¥é
- coding()ãã³ãã©ã使ãã°ããã£ã1è¡ã§æ¸ãï¼
on for_key(a_record, a_key) run script coding(a_record) & "'s |" & a_key & "|"
end for_key
- ã·ã³ãã«ãã¤åããæãããã¾ãã«å¦çãéãï¼
- ã¤ãã§ã«ãset_key()ãã³ãã©ã追å ãã¦ãããã
- ã¬ã³ã¼ãã«ããã¼ã¨å¤ã追å ããã®ã ãï¼ãã¼å¤ãåããªãä¸æ¸ãï¼
- ã¡ãªã¿ã«ãcoding()ãã³ãã©ã¯ä¸è¦ã§ããã
on set_key(a_record, a_key, a_value)
(run script "{|" & a_key & "|:" & a_value & "}") & a_record
end set_key
ä¸ä¾¿ãè£ã工夫ï¼JSONã«å¤æãã
- AppleScriptã«ããèªåãã°ã¤ã³ã»èªåå ¥åãå®ç¾ããauto_loginã¹ã¯ãªããã
- iPhoneã»iPadã§ãèªåãã°ã¤ã³ãå®ç¾ãããããããã¯ãã¼ã¯ã¬ããã«æå·åãããã°ã¤ã³æ å ±ãåãè¾¼ãå¿ è¦ããã£ãã
- ä½ãããã®ããã«ã¯ã¬ã³ã¼ãã«ä¿åãã¦ãããã°ã¤ã³æ å ±ãJSONã«å¤æãã¦JavaScriptå´ã«æ¸¡ããªãã¦ã¯ãªããªãã
- coding()ãã³ãã©ãç¥ããªãã£ãå½åãæ¶ã ã¨1é±éãå¯ã¦ãè¦ãã¦ãæ©ãã§ããã
coding()ãã³ãã©ä»¥é
- coding()ãã³ãã©ãè¦ããä»ããããªæ©ã¿ã¯çç¡ã§ããï¼
--ãªã¹ãã»ã¬ã³ã¼ãè¤åä½ãJSONã³ã¼ãã«å¤æãã
--ï¼ãªã¹ãã®{}ã[]ã«å¤æããããªã¹ãå ã®|ãåé¤ããï¼
on json_from(list_recode) coding(list_recode) _parse_list_record(result's items, 1) as text
replace(result, "|", "") end json_from
--json_fromããå¼ã³åºãããå帰çã«ãªã¹ãã®{}ã[]ã«å¤æãã
on _parse_list_record(str_list, i) set output to {} repeat
set v to str_list's item i
if v = "{" then
set box to _parse_list_record(str_list, i + 1) set output's end to box
set i to i + (box's length) else if v = "}" then
set obj to run script "{" & output & "}"
if obj's class = list then
return "[" & output & "]"
else
return "{" & output & "}"
end if
else
set output's end to v
set i to i + 1
end if
if i > str_list's length then exit repeat
end repeat
output
end _parse_list_record
- ãªã¹ãã»ã¬ã³ã¼ãè¤åãã¼ã¿ããJavaScriptãèªèããJSONã«ä¸çºã§å¤æã§ããã®ã ã
- ãªã¹ãã®æ¬å¼§ã[]ã«å¤æãã¦ãäºç´èªã¨ãããã£ã³ã°ãããã¼ã«ä»å ããã|ãåé¤ãã¦ããã
- ã¡ãªã¿ã«ãå³æ ¼ãªJSONå½¢å¼ã§ã¯ãªãã
- Safariã®JavaScriptã¯ã¡ããã¨èªèããã
JSONã«ã¤ãã¦
- JSONï¼ï¼JavaScript Object Notationï¼ã¯ãã®åã®ã¨ãããJavaScriptã¨é常ã«ç¸æ§ãè¯ãã
- do JavaScriptã§å®è¡ããã¨ãJSONã§è¿ãããã¼ã¿ã¯ããªã¹ãã»ã¬ã³ã¼ãè¤åãã¼ã¿ã¨ãã¦åå¾ã§ããã
tell application "Safari"
"var json={a:1,b:2,c:3};json['d']=4;json;"
do JavaScript result in document 1
end tell
--çµæï¼{d:4.0, b:2.0, c:3.0, a:1.0}
- æ°å¤ã«å°æ°è¡¨ç¤ºãä»å ããã¦ããã®ã¯æ°ã«ãªãããå¤ã¯åãã
- ã¾ããè¿ãå¤ã®ã¬ã³ã¼ãã®è¦ç´ ã®é åºãä¿è¨¼ãããªãããã ã
- ã¡ãªã¿ã«ãdo JavaScriptãå®è¡ããããã«ã¯ãSafariã§æä½ä¸ã¤ã®ã¿ãï¼ãããã¯ã¦ã£ã³ãã¦ãããã¥ã¡ã³ãï¼ãéãã¦ããå¿ è¦ãããã
- Safariã常ç¨ãã¦ããç°å¢ã§ã¯åé¡ãªããããããªãããä»ã®ãã©ã¦ã¶ã常ç¨ãã¦ããã¨ãSafariãèµ·åãã¦ãmake new documentããå¿ è¦ãããããã
ä¸ä¾¿ãè£ã工夫ï¼JavaScriptã¨ã®é£æº
- èªå¨ã«JSONã«å¤æã§ããããã«ãªã£ãä»ãAppleScriptã®ã¬ã³ã¼ãã«ä¸è¶³ããæ©è½ã¯ãJavaScriptã§è£ããã¨ãç°¡åã
- ä½ããæããã¦ããããã¹ããªã®ãã©ããã¯çå...ã*1
ã¬ã³ã¼ãã®ãã¼ãåãé¤ã
--ã¬ã³ã¼ãããæå®ãããã¼ãåãé¤ããã¬ã³ã¼ããè¿ãï¼å ã®ã¬ã³ã¼ãã¯å¤åããªãï¼
on reject_key(a_record, a_key) if a_record's class â record then return ""
set json to json_from(a_record) tell application "Safari"
if documents's number = 0 then make new document
"json=" & json & ";delete(json['" & a_key & "']);json;"
do JavaScript result in document 1
end tell
end reject_key
ã¬ã³ã¼ãã®ãã¼ããªã¹ãã§è¿ã
on record_keys(a_record) if a_record's class â record then return ""
set json to json_from(a_record) tell application "Safari"
if documents's number = 0 then make new document
"var a=" & json & ";var k=[];for(i in a){k.push(i)};k;"
do JavaScript result in document 1
end tell
end record_keys
ãã¦ã³ãã¼ã
以ä¸ã®ãã³ãã©ã¯ãä¾åããreplace()ãã³ãã©ãå«ãã¦ã_json.scptã«ã¾ã¨ããã
on split(sourceText, delimiter) --considering «constant conszkhk»--AppleScript2.0以éã¯ç¡å¹
if sourceText = "" then return {} set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to delimiter
set theList to text items of sourceText
set AppleScript's text item delimiters to oldDelimiters
return theList
--end considering
end split
on join(sourceList, delimiter) --considering «constant conszkhk»--AppleScript2.0以éã¯ç¡å¹
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to delimiter
set theText to sourceList as text
set AppleScript's text item delimiters to oldDelimiters
return theText
--end considering
end join
on replace(sourceText, text1, text2) join(split(sourceText, text1), text2) end replace
ææ
- 以ä¸ã®ããã«ãcoding()ãã³ãã©ã¯æ§ã ãªå¦çã®ãã¼ã¹ã¨ãªãæ©è½ãæä¾ããã
- coding()ãã³ãã©ã«ãã£ã¦ãä»ã¾ã§è«¦ãã¦ãããã¨ãå¯è½ã«ãªããæ°ããªä¸çãåºãã£ãã
- ãªãã¸ã§ã¯ããã½ã¼ã¹ã³ã¼ãã«å¤æããæ©è½ã¯é常ã«éè¦ã
- æ¬æ¥ã¯ãAppleScriptãåºæ¬æ©è½ã¨ãã¦åãã¦ãã¦æ¬²ããã
- ä¾å¤å¦çããã¾ãå©ç¨ãããã¨ã§ãæ³å以ä¸ã®æ©è½ã追å ã§ããã
- ãããã°ãRailsãRubyã®ä¾å¤å¦çãæãããæ´»ç¨ãããã¨ã§ã便å©ãªæ©è½ã追å ãã¦ããã®ãæãåºããã
- ä¾å¤å¦çãå©ç¨ãããã¨ã¯ãè¯ãããæªããï¼ã»ãã¥ãªãã£ãã¼ã«ãªã©ï¼ä½ããã®çªç ´å£ã«ãªããã¨ãå¤ãã
- AppleScriptã®å¼·ã¿ã¯ã対å¿ã¢ããªã±ã¼ã·ã§ã³ãæ軽ã«ã³ã³ããã¼ã«ã§ããã¨ããã
- ã¾ããdo shell scriptãdo JavaScriptãçµç±ãã¦ãæ§ã ãªè¨èªç°å¢ãæ´»ç¨ã§ããã¨ããã
- ããã¹ãå ¥åãã¢ã©ã¼ã表示ããªã¹ãé¸æããã¡ã¤ã«é¸æãªã©ã貧弱ã ãã©è¥å¹²ã®GUIãæä¾ããã
- ããããæ´»ç¨ãã¦ç°ãªãç°å¢ãç¹ãã¦é£æºãããã¨ããå½¹å²ãããã
- coding()ãã³ãã©ã«ãã£ã¦ãè¨èªç°å¢ã®é£æºãããå¼·åãããã®ã ã
- é常ã«çã®ããè¨èªã ããOSXç°å¢ã«ããã¦ã¯ããã®é£æºã®é åã¯ä»ã®ã©ã®è¨èªãæµããªãã
ãããAppleScriptã使ãç¶ããçç±ã«ãªã£ã¦ããã®ããã
*1:ãããªãJavascriptã使ãã°ããï¼ããããJavaScriptã®ã¿ã§ã¯ããã©ã¦ã¶ç°å¢ããå¤ã«ã¢ã¯ã»ã¹ã§ããªãï¼ã¨æã£ã¦ããï¼ã¨ããå¶ç´ãããã®ã§...ã