ラベル AppleScript の投稿を表示しています。 すべての投稿を表示
ラベル AppleScript の投稿を表示しています。 すべての投稿を表示

20260219

[AppleScript] 10進数を16進数に変換する(decimal to hexadecimal) ラングエージコードから国旗の絵文字を取得する

Acrobat Readerの単言語は
インストーラーのファイル名が
ラングエージコードと紐付いている
なので
そのラングエージコードから
対象の国の国旗の絵文字を取得できないか?

思って作成しました
ソースはこちらからダウンロードできます
https://editor.note.com/notes/n53fe820150da

こちらの記事で使った、カントリーコード(国コード)から
絵文字の国旗を取得する処理
10進数を16進数に変換する処理があるのですが
結局何が一番速度が速いのか?確認してみました。
AppleScript内での実行といった特殊な環境での実行ですなので
一般的な実行速度とは異なることに留意ください

Applescript 0.1秒
sh:0.2秒
zsh:0.3秒
perl:0.5秒
osascript js:1秒強
node :2秒弱
python3:2秒弱
ruby:3秒
swift:6秒
pwsh:18秒
複数回実行した平均 マシンのスペックにも依存があります

Applescript 0.1秒

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

[osascript]10進数を16進数に変換(Dec to Hex).scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+-----3----+----4----+----5----+----6----+----7--
004(*
005AppleScriptでラングエージコードから 国旗の絵文字を取得する
006処理速度目安
0070.1
008com.cocolog-nifty.quicktimer.icefloe *)
009----+----1----+----2----+-----3----+----4----+----5----+----6----+----7--
010use AppleScript version "2.8"
011use framework "Foundation"
012use framework "AppKit"
013use framework "CoreFoundation"
014use scripting additions
015property refMe : a reference to current application
016#処理開始時間
017set ocidStart to refMe's CFAbsoluteTimeGetCurrent()
018set listLangCode to {"ja_JP", "ca_ES", "cs_CZ", "da_DK", "de_DE", "en_GB", "en_US", "es_ES", "eu_ES", "fi_FI", "fr_FR", "hr_HR", "hu_HU", "it_IT", "ko_KR", "nl_NL", "no_NO", "pl_PL", "pt_BR", "ro_RO", "ru_RU", "sk_SK", "sl_SI", "sv_SE", "tr_TR", "uk_UA", "zh_CN", "zh_TW"} as list
019set listFlags to {} as list
020repeat with strLangCode in listLangCode
021   
022   
023   set ocidLocale to (refMe's NSLocale's alloc()'s initWithLocaleIdentifier:(strLangCode))
024   set ocidCountryCode to (ocidLocale's objectForKey:(refMe's NSLocaleCountryCode))
025   set strFlagEmoji to doCcode2FlagEmoji(ocidCountryCode) as text
026   
027   log strFlagEmoji
028   set end of listFlags to strFlagEmoji
029end repeat
030#処理終了時間
031set ocidEnd to refMe's CFAbsoluteTimeGetCurrent()
032set strTime to (ocidEnd - ocidStart) as text
033return (strTime & ":" & listFlags) as text
034########################
035#10進数を16進数に変換
036to doDec2Hex(argIntNo)
037   #大文字指定
038   set boolCaps to true as boolean
039   #10進数-->16進数の変換レコード
040   if boolCaps is true then
041      set recordDec2Hex to {|0|:"0", |1|:"1", |2|:"2", |3|:"3", |4|:"4", |5|:"5", |6|:"6", |7|:"7", |8|:"8", |9|:"9", |10|:"A", |11|:"B", |12|:"C", |13|:"D", |14|:"E", |15|:"F"} as record
042   else
043      set recordDec2Hex to {|0|:"0", |1|:"1", |2|:"2", |3|:"3", |4|:"4", |5|:"5", |6|:"6", |7|:"7", |8|:"8", |9|:"9", |10|:"a", |11|:"b", |12|:"c", |13|:"d", |14|:"e", |15|:"f"} as record
044   end if
045   set ocidDec2HexDict to refMe's NSMutableDictionary's alloc()'s init()
046   ocidDec2HexDict's setDictionary:(recordDec2Hex)
047   #次工程に回す余りを格納するArray
048   set ocidRemArray to refMe's NSMutableArray's alloc()'s init()
049   #引数を数値で再定義して
050   set inteRes to argIntNo as integer
051   #初回用のあまり値
052   set numValue to 9999999 as integer
053   set numDiv to 9999999 as integer
054   #余りが0になるから値が0になるまで繰り返し
055   repeat while (numDiv > 0)
056      #16で割って
057      set numDiv to (round (inteRes / 16) rounding down) as integer
058      #余りを
059      set numValue to inteRes - (numDiv * 16) as integer
060      #ループの次の回用に再定義
061      set inteRes to numDiv as integer
062      #Arrayに加えていく
063      ocidRemArray's addObject:(numValue)
064   end repeat
065   #逆順に並び替えて
066   set ocidRemArray to (ocidRemArray's reverseObjectEnumerator())'s allObjects()
067   set ocidHexString to refMe's NSMutableString's alloc()'s init()
068   (ocidHexString's appendString:("000000"))
069   #順番に
070   repeat with itemNo in ocidRemArray
071      #10進数を
072      set strItemNo to itemNo as text
073      #16進数に置き換えて
074      set strValue to (ocidDec2HexDict's objectForKey:(strItemNo))
075      (ocidHexString's appendString:(strValue))
076   end repeat
077   #ゼロサプレス6桁
078   set ocidLength to ocidHexString's |length|()
079   set numStart to (ocidLength - 5) as integer
080   set ocidRange to refMe's NSRange's NSMakeRange(numStart, 5)
081   set ocidReturnString to ocidHexString's substringWithRange:(ocidRange)
082   #HEX値の出来上がりを戻す
083   return ocidReturnString
084   
085end doDec2Hex
086##########################################
087#絵文字取得
088to doCcode2FlagEmoji(argCountryCode)
089   #念の為テキストにしてから再度NSStringにしておく
090   set strCountryCode to argCountryCode as text
091   set ocidCountryCode to refMe's NSString's stringWithString:(strCountryCode)
092   #オフセット値
093   set ocidOffSetA to refMe's NSString's stringWithString:("A")
094   set uniOffSetA to ocidOffSetA's characterAtIndex:(0)
095   set intOffSetA to uniOffSetA as integer
096   #サローゲートNO
097   set intSurrogateNo to 127462 as integer
098   #キャラクター上位
099   set uniHiChar to ocidCountryCode's characterAtIndex:(0)
100   set intHiCharDec to uniHiChar as integer
101   set intHiInd to (intSurrogateNo + intHiCharDec - intOffSetA) as integer
102   set strHiUniCode to doDec2Hex(intHiInd) as text
103   #キャラクター下位
104   set uniLwChar to ocidCountryCode's characterAtIndex:(1)
105   set intLwCharDec to uniLwChar as integer
106   set intLwInd to (intSurrogateNo + intLwCharDec - intOffSetA) as integer
107   set strLwUniCode to doDec2Hex(intLwInd) as text
108   #ユニコードサロゲートに整形
109   set strUniSurrogate to ("U+" & strHiUniCode & "U+" & strLwUniCode & "") as text
110   set ocidUniSurrogate to refMe's NSString's stringWithString:(strUniSurrogate)
111   set ocidFlagEmoji to (ocidUniSurrogate's stringByApplyingTransform:("Any-Hex/Unicode") |reverse|:(true))
112   if ocidFlagEmoji = (missing value) then
113      return "🏳️‍🌈"
114   end if
115   return ocidFlagEmoji as text
116   
117end doCcode2FlagEmoji
AppleScriptで生成しました


sh:0.2秒

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

[sh printf]10進数を16進数に変換(Dec to Hex).scpt
ソース
001########################
002#10進数を16進数に変換
003to doDec2Hex(argIntNo)
004   set strStdOut to (do shell script "printf \"%x\\\n\" " & argIntNo & "") as text
005   return strStdOut
006end doDec2Hex
AppleScriptで生成しました


zsh:0.3秒

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

[zsh]10進数を16進数に変換(Dec to Hex) .scpt
ソース
001########################
002#10進数を16進数に変換
003to doDec2Hex(argIntNo)
004   set strStdOut to (do shell script "/bin/zsh -c 'result=$(( [#16] " & argIntNo & " )) && echo ${result#*#}'") as text
005   
006   return strStdOut
007end doDec2Hex
AppleScriptで生成しました


perl:0.5秒

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

[perl]10進数を16進数に変換(Dec to Hex).scpt
ソース
001########################
002#10進数を16進数に変換
003to doDec2Hex(argIntNo)
004   set strStdOut to (do shell script "/usr/bin/perl -e 'printf(\"%x\", " & argIntNo & ")'") as text
005   return strStdOut
006end doDec2Hex
AppleScriptで生成しました


osascript js:1秒強

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

[osascript js]10進数を16進数に変換(Dec to Hex).scpt
ソース
001########################
002#10進数を16進数に変換
003to doDec2Hex(argIntNo)
004   set strJs to ("const strDecNo = " & argIntNo & ";function doDec2Hex() {const strHex = strDecNo.toString(16);return strHex;} doDec2Hex();") as text
005   set strCmd to ("/usr/bin/osascript -l JavaScript -e \"" & strJs & "\"") as text
006   set strStdOut to (do shell script strCmd) as text
007   return strStdOut as text
008end doDec2Hex
AppleScriptで生成しました


node :2秒弱

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

[node]10進数を16進数に変換(Dec to Hex).scpt
ソース
001########################
002#10進数を16進数に変換
003to doDec2Hex(argIntNo)
004   set strJs to ("console.log(Number(\"" & argIntNo & "\").toString(16))") as text
005   set strCmd to ("/usr/local/bin/node -e \"" & strJs & "\"") as text
006   set strStdOut to (do shell script strCmd) as text
007   return strStdOut as text
008end doDec2Hex
AppleScriptで生成しました


python3:2秒弱

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

[Python]10進数を16進数に変換(Dec to Hex).scpt
ソース
001########################
002#10進数を16進数に変換
003to doDec2Hex(argIntNo)
004   set strStdOut to (do shell script "/usr/bin/python3 -c \"print(f\\\"{" & argIntNo & ":x}\\\");\"") as text
005   return strStdOut
006end doDec2Hex
AppleScriptで生成しました


ruby:3秒

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

[ruby]10進数を16進数に変換(Dec to Hex) .scpt
ソース
001
002########################
003#10進数を16進数に変換
004to doDec2Hex(argIntNo)
005   set strStdOut to (do shell script "/usr/bin/ruby -e 'puts " & argIntNo & ".to_s(16)'") as text
006   return strStdOut
007end doDec2Hex
AppleScriptで生成しました


swift:6秒

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

[swift]10進数を16進数に変換(Dec to Hex) .scpt
ソース
001########################
002#10進数を16進数に変換
003to doDec2Hex(argIntNo)
004   set strStdOut to (do shell script "/usr/bin/swift -e 'let numDec = " & argIntNo & ";let strHex = String(numDec, radix: 16);print(strHex);'") as text
005   return strStdOut
006end doDec2Hex
AppleScriptで生成しました


pwsh:18秒

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

[pwsh]10進数を16進数に変換(Dec to Hex) .scpt
ソース
001########################
002#10進数を16進数に変換
003to doDec2Hex(argIntNo)
004   set strStdOut to (do shell script "/usr/local/bin/pwsh -c '(" & argIntNo & ").ToString(\"x\")'") as text
005   return strStdOut
006end doDec2Hex
AppleScriptで生成しました