AppleScript名:すべてのLocaleから各種情報を取得 |
— Created 2015-10-03 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set allLocaleIdentifiers to (current application’s NSLocale’s availableLocaleIdentifiers()) as list set cList to {} repeat with i in allLocaleIdentifiers set j to contents of i set tmpLoc to (current application’s NSLocale’s alloc()’s initWithLocaleIdentifier:j) set aLangCode to (tmpLoc’s objectForKey:(current application’s NSLocaleLanguageCode)) as text set aCountryCode to (tmpLoc’s objectForKey:(current application’s NSLocaleCountryCode)) as text set aCCYSymbol to (tmpLoc’s objectForKey:(current application’s NSLocaleCurrencySymbol)) as text set aLocID to (tmpLoc’s objectForKey:(current application’s NSLocaleIdentifier)) as text –set aCountryName to (tmpLoc’s displayNameForKey:(current application’s NSLocaleCountryCode)) set locIDDisplayName to (tmpLoc’s displayNameForKey:(current application’s NSLocaleIdentifier) value:aLocID) as text set the end of cList to {aLocID, aLangCode, aCountryCode, aCCYSymbol, locIDDisplayName} end repeat cList –> {{"eu", "eu", "missing value", "¤", "euskara"},…. {"ja_JP", "ja", "JP", "¥", "日本語 (日本)"}, ….. {"ja", "ja", "missing value", "¤", "日本語"}, ………., {"en_US", "en", "US", "$", "English (United States)"}, {"en_US_POSIX", "en", "US", "$", "English (United States, Computer)"}, {"zh-Hans", "zh", "missing value", "¤", "中文(简体)"},….. {"zh-Hant", "zh", "missing value", "¤", "中文(繁體)"}, ….{"zh-Hans_HK", "zh", "HK", "HK$", "中文(简体、中国香港特别行政区)"},} |
タグ: 10.13savvy
Current Localeから各種情報を取得する
AppleScript名:Current Localeから各種情報を取得する |
— Created 2016-10-12 by Takaaki Naganoya — 2016 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set curLocale to current application’s NSLocale’s currentLocale() set aDS1 to curLocale’s objectForKey:(current application’s NSLocaleDecimalSeparator) –> (NSString) "." set aDS2 to curLocale’s objectForKey:(current application’s NSLocaleGroupingSeparator) –> (NSString) "," set aDS3 to curLocale’s objectForKey:(current application’s NSLocaleCurrencySymbol) –> (NSString) "¥" set aDS4 to curLocale’s objectForKey:(current application’s NSLocaleCurrencyCode) –> (NSString) "JPY" set aDS5 to curLocale’s objectForKey:(current application’s NSLocaleCollatorIdentifier) –> (NSString) "ja-JP" set aDS6 to curLocale’s objectForKey:(current application’s NSLocaleQuotationBeginDelimiterKey) –> (NSString) "「" set aDS7 to curLocale’s objectForKey:(current application’s NSLocaleQuotationEndDelimiterKey) –> (NSString) "」" set aDS8 to curLocale’s objectForKey:(current application’s NSLocaleAlternateQuotationBeginDelimiterKey) –> (NSString) "『" set aDS9 to curLocale’s objectForKey:(current application’s NSLocaleAlternateQuotationEndDelimiterKey) –> (NSString) "』" set aDS10 to curLocale’s objectForKey:(current application’s NSLocaleIdentifier) –> (NSString) "ja_JP" set aDS11 to curLocale’s objectForKey:(current application’s NSLocaleLanguageCode) –> (NSString) "ja" set aDS12 to curLocale’s objectForKey:(current application’s NSLocaleCountryCode) –> (NSString) "JP" set aDS13 to curLocale’s objectForKey:(current application’s NSLocaleScriptCode) –> missing value set aDS14 to curLocale’s objectForKey:(current application’s NSLocaleVariantCode) –> missing value set aDS15 to curLocale’s objectForKey:(current application’s NSLocaleExemplarCharacterSet) –> (__NSCFCharacterSet) <__NSCFCharacterSet: 0x60800124d890> set aDS16 to curLocale’s objectForKey:(current application’s NSLocaleCalendar) –> (_NSCopyOnWriteCalendarWrapper) <_NSCopyOnWriteCalendarWrapper: 0x610000232ca0> set aDS17 to curLocale’s objectForKey:(current application’s NSLocaleCollationIdentifier) –> missing value set aDS18 to curLocale’s objectForKey:(current application’s NSLocaleUsesMetricSystem) –> (NSNumber) 1 set aDS19 to curLocale’s objectForKey:(current application’s NSLocaleMeasurementSystem) –> (NSString) "Metric" |
Locale情報を取得する
AppleScript名:Locale情報を取得する |
— Created 2015-09-11 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set aLocale to (current application’s NSLocale’s currentLocale’s objectForKey:(current application’s NSLocaleIdentifier)) as list of string or string –as anything –> "ja_JP" set aLangList to (current application’s NSLocale’s preferredLanguages()) as list of string or string –as anything –> {"ja", "en-US", "en-GB", "fr", "en"} set aLocList to (current application’s NSLocale’s availableLocaleIdentifiers()) as list of string or string –as anything –> {"eu", "hr_BA", "en_CM", "rw_RW", "en_SZ", "tk_Latn", "uz_Arab", "he_IL", "ar", "en_PN", "as", "en_NF", "rwk_TZ", "zh_Hant_TW", "gsw_LI", "th_TH", "ta_IN", "es_EA", "fr_GF", "ar_001", "en_RW", "tr_TR", "de_CH", "ee_TG", "en_NG", "fr_TG", "az", "fr_SC", "es_HN", "en_AG", "ru_KZ", "gsw", "dyo", "so_ET", "zh_Hant_MO", "de_BE", "km_KH", "my_MM", "mgh_MZ", "ee_GH", "es_EC", "kw_GB", "rm_CH", "en_ME", "nyn", "mk_MK", "bs_Cyrl_BA", "ar_MR", "en_BM", "ms_Arab", "en_AI", "gl_ES", "en_PR", "ha_Latn_GH", "ne_IN", "or_IN", "khq_ML", "en_MG", "pt_TL", "en_LC", "ta_SG", "jmc_TZ", "om_ET", "lv_LV", "es_US", "en_PT", "vai_Latn_LR", "to_TO", "en_NL", "cgg_UG", "ta", "en_MH", "iu_Cans_CA", "zu_ZA", "shi_Latn_MA", "brx_IN", "ar_KM", "en_AL", "te", "chr_US", "yo_BJ", "fr_VU", "pa", "tg", "ks_Arab", "kea", "te_IN", "th", "fr_RE", "ur_IN", "yo_NG", "ti", "guz_KE", "tk", "kl_GL", "ksf_CM", "mua_CM", "lag_TZ", "fr_TN", "es_PA", "pl_PL", "to", "hi_IN", "dje_NE", "es_GQ", "kok_IN", "pl", "tr", "bem", "ha", "ckb", "lg", "fr_GN", "en_PW", "en_NO", "nyn_UG", "sr_Latn_RS", "pa_Guru", "he", "swc_CD", "ug_Arab", "lu_CD", "mgo_CM", "sn_ZW", "en_BS", "ps_AF", "da", "ms_Latn_SG", "ps", "ln", "pt", "iu_Cans", "hi", "lo", "ebu", "de", "gu_IN", "seh", "en_CX", "en_ZM", "tzm_Latn_MA", "fr_HT", "fr_GP", "lt", "lu", "ln_CD", "vai_Latn", "el_GR", "lv", "en_KE", "sbp", "hr", "en_CY", "es_GT", "twq_NE", "zh_Hant_HK", "kln_KE", "fr_GQ", "chr", "hu", "es_UY", "fr_CA", "en_NR", "mer", "shi", "es_PE", "fr_SN", "bez", "sw_TZ", "kkj", "hy", "kk_Cyrl_KZ", "en_CZ", "teo_KE", "teo", "dz_BT", "ar_JO", "mer_KE", "khq", "ln_CF", "nn_NO", "en_MO", "ar_TD", "dz", "ses", "en_BW", "en_AS", "ar_IL", "ms_Latn_BN", "bo_CN", "nnh", "teo_UG", "hy_AM", "ln_CG", "sr_Latn_BA", "en_MP", "ksb_TZ", "ar_SA", "ar_LY", "en_AT", "so_KE", "fr_CD", "af_NA", "en_NU", "es_PH", "en_KI", "en_JE", "lkt", "en_AU", "fa_IR", "uz_Latn_UZ", "ky_Cyrl", "zh_Hans_CN", "ewo_CM", "fr_PF", "ca_IT", "en_BZ", "ar_KW", "pt_GW", "fr_FR", "am_ET", "en_VC", "fr_DJ", "fr_CF", "es_SV", "en_MS", "pt_ST", "ar_SD", "luy_KE", "swc", "de_LI", "fr_CG", "zh_Hans_SG", "en_MT", "ewo", "af_ZA", "om_KE", "nl_SR", "es_ES", "es_DO", "ar_IQ", "fr_CH", "nnh_CM", "es_419", "en_MU", "en_US_POSIX", "yav_CM", "luo_KE", "dua_CM", "et_EE", "en_IE", "ak_GH", "rwk", "es_CL", "kea_CV", "fr_CI", "fr_BE", "en_NZ", "ky_Cyrl_KG", "en_LR", "en_KN", "nb_SJ", "sg", "sr_Cyrl_RS", "ru_RU", "en_ZW", "sv_AX", "si", "ga_IE", "en_VG", "sk", "agq_CM", "fr_BF", "naq_NA", "sl", "en_MW", "mr_IN", "az_Latn", "en_LS", "de_AT", "ka", "sn", "sr_Latn_ME", "fr_NC", "so", "is_IS", "twq", "ig_NG", "sq", "fo_FO", "sr", "tzm", "ga", "om", "en_LT", "bas_CM", "ki", "nl_BE", "ar_QA", "sv", "kk", "sw", "es_CO", "az_Latn_AZ", "rn_BI", "or", "kl", "ca", "en_VI", "km", "kn", "en_LU", "fr_SY", "ar_TN", "en_JM", "fr_PM", "ko", "fr_NE", "fr_MA", "gl", "ru_MD", "saq_KE", "ks", "fr_CM", "gv_IM", "fr_BI", "en_LV", "ks_Arab_IN", "es_NI", "en_GB", "kw", "nl_SX", "dav_KE", "tr_CY", "ky", "en_UG", "tzm_Latn", "en_TC", "nus_SD", "ar_EG", "fr_BJ", "gu", "es_PR", "fr_RW", "sr_Cyrl_BA", "gv", "fr_MC", "cs", "bez_TZ", "es_CR", "asa_TZ", "ar_EH", "ms_Arab_BN", "mn_Cyrl", "sbp_TZ", "ha_Latn_NE", "lt_LT", "mfe", "en_GD", "cy", "ca_FR", "es_BO", "fr_BL", "bn_IN", "uz_Cyrl_UZ", "az_Cyrl", "en_IM", "sw_KE", "en_SB", "ur_PK", "pa_Arab", "haw_US", "ar_SO", "en_IN", "ha_Latn", "fil", "fr_MF", "en_WS", "es_CU", "ja_JP", "en_SC", "en_IO", "pt_PT", "en_HK", "en_GG", "fr_MG", "de_LU", "ms_Latn_MY", "tg_Cyrl", "en_SD", "shi_Tfng", "ln_AO", "ug_Arab_CN", "as_IN", "en_GH", "ro_RO", "jgo_CM", "dua", "en_UM", "en_SE", "kn_IN", "en_KY", "vun_TZ", "kln", "en_GI", "ca_ES", "rof", "pt_CV", "kok", "pt_BR", "ar_DJ", "zh", "fi_FI", "tg_Cyrl_TJ", "es_PY", "ar_SS", "mua", "sr_Cyrl_ME", "vai_Vaii_LR", "en_001", "xog_UG", "en_TK", "si_LK", "en_SG", "nl_NL", "vi", "sv_SE", "pt_AO", "fr_DZ", "ca_AD", "xog", "en_IS", "nb", "seh_MZ", "es_AR", "sk_SK", "en_SH", "ti_ER", "nd", "az_Cyrl_AZ", "zu", "ne", "nd_ZW", "el_CY", "en_IT", "nl_BQ", "da_GL", "ja", "rm", "fr_ML", "rn", "en_VU", "rof_TZ", "ro", "ebu_KE", "ru_KG", "en_SI", "sg_CF", "mfe_MU", "nl", "brx", "bs_Latn", "fa", "zgh_MA", "en_GM", "shi_Latn", "en_FI", "nn", "en_EE", "ru", "kam_KE", "vai_Vaii", "ar_ER", "ti_ET", "rw", "ff", "luo", "fa_AF", "ha_Latn_NG", "nl_CW", "en_HR", "en_FJ", "fi", "pt_MO", "be", "en_US", "en_TO", "en_SK", "bg", "ru_BY", "it_IT", "ml_IN", "gsw_CH", "fo", "sv_FI", "en_FK", "nus", "ta_LK", "vun", "sr_Latn", "fr", "en_SL", "bm", "ar_BH", "guz", "bn", "bo", "ar_SY", "lo_LA", "ne_NP", "uz_Latn", "be_BY", "es_IC", "sr_Latn_XK", "ar_MA", "pa_Guru_IN", "br", "luy", "kde_TZ", "bs", "hu_HU", "ar_AE", "en_HU", "zh_Hans", "en_FM", "sq_AL", "ko_KP", "en_150", "en_DE", "fr_MQ", "en_CA", "en_TR", "ro_MD", "es_VE", "fr_WF", "mt_MT", "kab", "nmg_CM", "ru_UA", "fr_MR", "tk_Latn_TM", "zh_Hans_MO", "mn_Cyrl_MN", "bs_Cyrl", "sw_UG", "ko_KR", "en_DG", "bo_IN", "en_CC", "shi_Tfng_MA", "lag", "it_SM", "en_TT", "ms_Arab_MY", "sq_MK", "ms_Latn", "bem_ZM", "kde", "ar_OM", "cgg", "bas", "kam", "zh_Hant", "es_MX", "en_GU", "fr_MU", "fr_KM", "ar_LB", "en_BA", "en_TV", "sr_Cyrl", "dje", "kab_DZ", "fil_PH", "vai", "hr_HR", "bs_Latn_BA", "nl_AW", "dav", "so_SO", "ar_PS", "en_FR", "uz_Cyrl", "ff_SN", "en_BB", "ki_KE", "naq", "en_SS", "mg_MG", "mas_KE", "en_RO", "en_PG", "mgh", "dyo_SN", "mas", "agq", "bn_BD", "haw", "nb_NO", "da_DK", "en_DK", "saq", "ug", "cy_GB", "fr_YT", "jmc", "ses_ML", "en_PH", "de_DE", "ar_YE", "bm_ML", "yo", "lkt_US", "uz_Arab_AF", "jgo", "uk", "sl_SI", "en_CH", "asa", "lg_UG", "mgo", "id_ID", "en_NA", "en_GY", "zgh", "pt_MZ", "fr_LU", "kk_Cyrl", "mas_TZ", "ur", "en_DM", "ta_MY", "en_BE", "mg", "fr_GA", "ka_GE", "nmg", "en_TZ", "eu_ES", "ar_DZ", "id", "so_DJ", "yav", "mk", "pa_Arab_PK", "ml", "en_ER", "ig", "mn", "ksb", "uz", "vi_VN", "ii", "en_PK", "ee", "mr", "ms", "en_ES", "sq_XK", "it_CH", "mt", "en_CK", "br_FR", "sr_Cyrl_XK", "ksf", "en_SX", "bg_BG", "en_PL", "af", "el", "cs_CZ", "fr_TD", "zh_Hans_HK", "is", "my", "en", "it", "ii_CN", "eo", "iu", "en_ZA", "en_AD", "ak", "en_RU", "kkj_CM", "am", "es", "et", "uk_UA"} |
指定言語環境(Locale)の月名、曜日名を取得する
AppleScript名:指定言語環境(Locale)の月名、曜日名を取得する |
use AppleScript version "2.4" use scripting additions use framework "Foundation" set aList to getLocalizedDaynames("en_US") –> {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"} set aList to getLocalizedDaynames("fr_FR") –> {"Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"} set aList to getLocalizedDaynames("ja_JP") –> {"日曜日", "月曜日", "火曜日", "水曜日", "木曜日", "金曜日", "土曜日"} set aList to getLocalizedDaynames("zh-Hans") –> {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"} set bList to getLocalizedMonthnames("en_US") –> {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"} set bList to getLocalizedMonthnames("fr_FR") –> {"janvier", "février", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "décembre"} set bList to getLocalizedMonthnames("ja_JP") –> {"1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"} set bList to getLocalizedMonthnames("zh-Hans") –> {"一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"} –ローカライズされた曜日名称を返す on getLocalizedDaynames(aLoc) set df to current application’s NSDateFormatter’s alloc()’s init() df’s setLocale:(current application’s NSLocale’s localeWithLocaleIdentifier:aLoc) set dayNames to df’s standaloneWeekdaySymbols() as list return dayNames end getLocalizedDaynames –ローカライズされた月名称を返す on getLocalizedMonthnames(aLoc) set df to current application’s NSDateFormatter’s alloc()’s init() df’s setLocale:(current application’s NSLocale’s localeWithLocaleIdentifier:aLoc) set monthNames to df’s standaloneMonthSymbols() as list return monthNames end getLocalizedMonthnames |
秒以下の時間待ちを計測
AppleScript名:秒以下の時間待ちを計測 |
— Created 2015-09-05 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set a1Dat to current application’s NSDate’s timeIntervalSinceReferenceDate() current application’s NSThread’s sleepForTimeInterval:("0.001" as real) –delay 0.001 set b1Dat to current application’s NSDate’s timeIntervalSinceReferenceDate() set c1Dat to b1Dat – a1Dat –> 0.001403987408 –> 0.001309990883 –> 0.001239955425 |
NSRangeの拡張、隣接検出
NSRangeの隣接検出を行うAppleScriptです。
NSRangeやNSRectの重なり検出機能はとても強力で、OLD Style AppleScriptの時代では考えられなかったほどの処理の柔軟性を得られています。
強力ではあるものの、機能が素朴であることもまた事実であります。AとBの2つのNSRangeが重なっていることしか検出できないのはつくづく残念です。
不満な点の筆頭に挙げられるのが、「隣接検出」機能がないことです。
たとえば、NSAttributedStringに対して書式を取得する処理を行うと、3点リーダー文字(「…」)や感嘆符(「!」)の箇所で、書式情報が変わっていないのに分割して取得されてしまいます。バグではないものの、あまり好ましくない挙動です。
こうした箇所は、Rangeとしては「隣接」しているわけで、同一書式のデータが「隣接」している場合には、続きのデータとしてまとめられた方がいいに決まっています。
そこで冒頭のNSRangeの隣接検出という話になるわけですが……NSRangeが1次元座標上の始点と終点の2つの情報で構成されているために、これらの情報を「拡張」します。
set aRange to current application’s NSMakeRange(10, 80)
というNSRangeを作ると、
–> {location:10, |length|:80}
始点(10)と、始点からの長さが80で、終点が90になります。
これを「拡張」すると、
–> {location:9, |length|:82}
となります。NSRangeが「隣接」は検出できなくても、重なり合いは検出できるため、「拡張」すれば、隣接時には「重なっている」と判定できることになります。
あとは、リストに入れたNSRangeのうち重なり合っているものをループで検出するルーチンを書けば、大量のNSRange同士の隣接を処理できることでしょう。
AppleScript名:NSRangeの拡張、隣接検出 |
— Created 2018-1-12 by Takaaki Naganoya — 2018 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set aRange to current application’s NSMakeRange(10, 80) –original range –> {location:10, |length|:80} set bRange to extendRange(aRange) of me –Extended range –> {location:9, |length|:81} set cRange to current application’s NSMakeRange(90, 30) –compare target range –> {location:90, |length|:30} set aRes to current application’s NSIntersectionRange(aRange, cRange) –> {location:0, |length|:0}–Not adjacent set bRes to current application’s NSIntersectionRange(bRange, cRange) –> {location:90, |length|:1}–Adjacent –NSRangeの開始点とサイズを拡張 on extendRange(aRange) set aLoc to location of aRange set aLen to |length| of aRange if aLoc > 0 then set aLoc to aLoc – 1 end if set aLen to aLen + 2 set bRange to current application’s NSMakeRange(aLoc, aLen) return bRange end extendRange |
MaxRangeを取得する
AppleScript名:MaxRangeを取得する |
use AppleScript version "2.4" use scripting additions use framework "Foundation" set aRange to current application’s NSMakeRange(10, 80) –original range set aMaxX to current application’s NSMaxRange(aRange) –> 90 –set aMinX to current application’s NSMinRange(aRange)–最小はない |
NSRangeの基礎的な処理
AppleScript名:NSRangeの基礎的な処理 |
— Created 2017-12-12 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set aRange to current application’s NSMakeRange(0, 100) –> {location:0, length:100} set maxRange to current application’s NSMaxRange(aRange) –最大値 –> 100 set rangeStr to (current application’s NSStringFromRange(aRange)) as string –> "{0, 100}" set bRange to current application’s NSRangeFromString(rangeStr) –> {location:0, length:100} set cRange to current application’s NSMakeRange(50, 80) set cRes to current application’s NSEqualRanges(aRange, cRange) –> false set dRes to current application’s NSEqualRanges(aRange, bRange) –> true set eRes to current application’s NSIntersectionRange(aRange, cRange) –> {location:50, length:50} set fRes to current application’s NSLocationInRange(50, aRange) –> true set gRes to current application’s NSLocationInRange(120, aRange) –> false set hRes to current application’s NSUnionRange(aRange, cRange) –> {location:0, length:130} |
数値に対して、小数点以下の数値の切り上げ、切り下げ
AppleScript名:数値に対して、小数点以下の数値の切り上げ、切り下げ |
— Created 2017-08-12 by Takaaki Naganoya — Modified 2017-08-28 by Shane Stanley use AppleScript version "2.4" use scripting additions use framework "Foundation" property NSString : a reference to current application’s NSString property NSNumber : a reference to current application’s NSNumber property NSNumberFormatter : a reference to current application’s NSNumberFormatter property NSNumberFormatterRoundUp : a reference to current application’s NSNumberFormatterRoundUp property NSNumberFormatterRoundDown : a reference to current application’s NSNumberFormatterRoundDown set a to 0.909661 set a1Res to roundingDownNumStr(a, 2) of me –> 0.9 on roundingDownNumStr(aNum as string, aDigit as integer) set a to NSString’s stringWithString:aNum set aa to a’s doubleValue() set aFormatter to NSNumberFormatter’s alloc()’s init() aFormatter’s setMaximumFractionDigits:aDigit aFormatter’s setRoundingMode:(NSNumberFormatterRoundDown) set aStr to aFormatter’s stringFromNumber:aa return (aStr as text) as real end roundingDownNumStr on roundingUpNumStr(aNum as string, aDigit as integer) set a to NSString’s stringWithString:aNum set aa to a’s doubleValue() set aFormatter to NSNumberFormatter’s alloc()’s init() aFormatter’s setMaximumFractionDigits:aDigit aFormatter’s setRoundingMode:(NSNumberFormatterRoundUp) set aStr to aFormatter’s stringFromNumber:aa return (aStr as text) as real end roundingUpNumStr |
数値文字列に対して、少数点以下の数値の切り上げ、切り下げ v2
AppleScript名:数値文字列に対して、少数点以下の数値の切り上げ、切り下げ v2 |
— Created 2017-08-12 by Takaaki Naganoya — Modified 2017-08-28 by Shane Stanley use AppleScript version "2.4" use scripting additions use framework "Foundation" property NSString : a reference to current application’s NSString property NSNumber : a reference to current application’s NSNumber property NSNumberFormatter : a reference to current application’s NSNumberFormatter property NSNumberFormatterRoundUp : a reference to current application’s NSNumberFormatterRoundUp property NSNumberFormatterRoundDown : a reference to current application’s NSNumberFormatterRoundDown set a to "0.9096617698669434" set a1Res to roundingDownNumStr(a, 2) of me set b to "0.0001830748806241899" set b1Res to roundingDownNumStr(b, 2) of me return {a1Res, b1Res} –> {0.9, 0.0} on roundingDownNumStr(aNum as string, aDigit as integer) set a to NSString’s stringWithString:aNum set aa to a’s doubleValue() set aFormatter to NSNumberFormatter’s alloc()’s init() aFormatter’s setMaximumFractionDigits:aDigit aFormatter’s setRoundingMode:(NSNumberFormatterRoundDown) set aStr to aFormatter’s stringFromNumber:aa return (aStr as text) as real end roundingDownNumStr on roundingUpNumStr(aNum as string, aDigit as integer) set a to NSString’s stringWithString:aNum set aa to a’s doubleValue() set aFormatter to NSNumberFormatter’s alloc()’s init() aFormatter’s setMaximumFractionDigits:aDigit aFormatter’s setRoundingMode:(NSNumberFormatterRoundUp) set aStr to aFormatter’s stringFromNumber:aa return (aStr as text) as real end roundingUpNumStr |
数値書式関連
AppleScript名:数値書式関連 |
use AppleScript version "2.4" use framework "Foundation" use scripting additions set theNumber to 1.234567890123E+12 set theResult to formatNumber_(theNumber) –> "1,234,567,890,123" set aRes to textFromNumber_forLocale_(1.2345678E+4, "fr") –> "douze-mille-trois-cent-quarante-cinq virgule six sept huit" set aRes to textFromNumber_forLocale_(1.2345678E+4, "ja") –> "一万二千三百四十五・六七八" set aRes to formatNumber_usingFormat_(1.2345678E+4, "#,###.00;0.00;(#,##0.00)") –> "12,345.68" on formatNumber:theNumber set theFormatter to current application’s NSNumberFormatter’s new() theFormatter’s setNumberStyle:(current application’s NSNumberFormatterDecimalStyle) set theResult to theFormatter’s stringFromNumber:theNumber return theResult as text end formatNumber: on textFromNumber:theNumber forLocale:localeString set theFormatter to current application’s NSNumberFormatter’s new() theFormatter’s setNumberStyle:(current application’s NSNumberFormatterSpellOutStyle) set theLocale to current application’s NSLocale’s localeWithLocaleIdentifier:localeString theFormatter’s setLocale:theLocale set theResult to theFormatter’s stringFromNumber:theNumber return theResult as text end textFromNumber:forLocale: on formatNumber:theNumber usingFormat:formatString set theFormatter to current application’s NSNumberFormatter’s new() theFormatter’s setFormat:formatString theFormatter’s setLocalizesFormat:false set theResult to theFormatter’s stringFromNumber:theNumber return theResult as text end formatNumber:usingFormat: |
大きな数値同士を足し算する
巨大な数値同士を加算するAppleScriptです。
AppleScriptの数値型データが±99999999程度で指数表示になってしまうため、巨大な数値を扱うのには向いていないのですが、直接数値で扱わずに文字列として保持すれば問題はありません。
数値文字同士の計算についても、bcコマンドを使うことで四則演算+α程度は行えるので、あまり問題はないでしょう。bcコマンドを呼び出すオーバーヘッドはそれなりにあるので、すべての数値を文字列で保持したりするのは得策ではありません。必要な箇所だけ使う、といったところです。
AppleScript名:大きな数値同士を足し算する |
set a to "100000000000000000" set b to "200000000000000000" set c to addLargeNumber(a, b) of me on addLargeNumber(aNum, bNum) set aNumStr to aNum as string set bNumStr to bNum as string set aCMD to "echo \" scale=10; " & aNumStr & "+" & bNumStr & " \" | bc" set aRes to do shell script aCMD return aRes end addLargeNumber |
与えられた数が素数かどうかチェック(PrimeNumberHelper)
与えられた数値が素数かどうかチェックを行うAppleScriptです。
素数チェックには、外部フレームワーク「primeNumKit.framework」を呼び出しています。
素数チェックや素因数分解は意外と実務でも必要になる機会が多く、高速な計算ルーチンが使えると便利です。
AppleScript名:与えられた数が素数かどうかチェック(PrimeNumberHelper) |
— Created 2017-05-18 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "primeNumKit" –https://github.com/NazarYavornytskyy/prime-number-objc set aPrime to current application’s PrimeNumberHelper’s alloc()’s init() set a to 75777773 set aRes to (aPrime’s isPrime:a) as boolean |
簡単な素因数分解v5
与えられた数値の素因数分解を行うAppleScriptです。
素数チェックには、数値が小さい場合にはOLD Style AppleScriptの計算ルーチンを呼び出し、数値が大きい(=計算に時間がかかる)場合には外部フレームワーク「primeNumKit.framework」を呼び出すようにしています。
AppleScriptからのCocoaの機能呼び出しにも時間がかかるので、細かい処理を頻繁に呼び出すと意外とAppleScriptだけで処理したほうが速かったりします(素数チェックを行うためにループで割り算を行いますが、最大値は2から目的値の1/2まででよく、こうした地道な改善が予想外に効いています)。
そのため、計算量を見てどちらで処理するかを判定しています。
AppleScript名:簡単な素因数分解v5 |
— Created 2017-05-18 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "primeNumKit" –https://github.com/NazarYavornytskyy/prime-number-objc set aRes to getPrimeFactor(56) of me –> {2, 2, 2, 7} set aRes to getPrimeFactor(54) of me –> {2, 3, 3, 3} set aRes to getPrimeFactor(9999991) of me –> {9999991} –素数 –与えられた数を素因数分解する on getPrimeFactor(aTargNum) copy aTargNum to a set pFactList to {} repeat set pRes to checkPrimeNumber(a) of me if pRes = true then set the end of pFactList to a return pFactList end if set aFactor to checkFactor(a) of me set the end of pFactList to aFactor set a to a div aFactor end repeat end getPrimeFactor –素数チェック on checkPrimeNumber(aNum) if aNum < 10000 then –0.25秒以上かかりそうな処理はCocoaで処理(実測値ベース) return checkPrimeNumberSub1(aNum) of me else set aPrime to current application’s PrimeNumberHelper’s alloc()’s init() return (aPrime’s isPrime:aNum) as boolean end if end checkPrimeNumber –小さい数の場合の素数検出 on checkPrimeNumberSub1(aNum) repeat with i from 2 to (aNum div 2) if aNum mod i is equal to 0 then return i –false end if end repeat return true end checkPrimeNumberSub1 –素因数チェック on checkFactor(aNum) repeat with i from 2 to (aNum div 2) if aNum mod i is equal to 0 then return i end if end repeat end checkFactor |
指定のコード体系の全パターンのコードを生成 v5
AppleScript名:指定のコード体系の全パターンのコードを生成 v5 |
–v5 外部からプロパティで与えられたルールから、初期値となる「最小値」を自前で計算するように変更 –v4 桁ごとにサブルーチンを設けるのではなく、再帰処理で1つのルーチンを多重呼び出しするように変更 –v3 コードのルールを外部供給する構成にした(処理ロジックとルールの分離が完了) –v2 各桁の最大値と最小値をプロパティで持たせるテスト –v1 各桁のインクリメント用のサブルーチンを作成し、ルールを各サブルーチン側でハードコーディングする(正しく動く) script spd property aList : {} property aRuleList : {{1, 2}, {1, 3}, {0, 1}, {1, 4}, {1, 8}} –各桁の{最小値, 最大値}ペアのリスト property aRuleLen : length of aRuleList end script set aList of spd to {} –initilaize set initNum to getMinNum() of me –本ルール下における最小値 set the end of aList of spd to initNum copy initNum to aNum repeat set aRes to incDigit(aNum, 1) of me if aRes = false then exit repeat end if set the end of aList of spd to aRes copy aRes to aNum end repeat –return length of (aList of spd) return (aList of spd) –与えられたルール下における最小値をルールリストから求める on getMinNum() –桁数が合っているだけのダミー数字を、適切な桁数作成する(例:11111) set tmpNumStr to "" repeat (aRuleLen of spd) times set tmpNumStr to tmpNumStr & "1" end repeat set tmpNum to tmpNumStr as integer –ルールから各桁の最小値を取り出して、各桁に設定する repeat with i from 1 to (aRuleLen of spd) set aDigNum to item 1 of item i of (aRuleList of spd) set tmpNum to setDigit(tmpNum, i, aDigNum) of me end repeat return tmpNum end getMinNum –繰り上がり処理(再帰呼び出しで使用) on incDigit(aNum, aDigit) set {thisMin, thisMax} to item ((aRuleLen of spd) – aDigit + 1) of (aRuleList of spd) set aTarget to getDigit(aNum, aDigit) of me if aTarget = thisMax then if aDigit = (aRuleLen of spd) then –オーバーフロー(桁あふれ)エラーを返す return false end if set bNum to incDigit(aNum, aDigit + 1) of me if bNum = false then return false set bNum to setDigit(bNum, aDigit, thisMin) of me else set aTarget to aTarget + 1 set bNum to setDigit(aNum, aDigit, aTarget) of me end if return bNum end incDigit –指定数値のうち指定桁の数字を返す on getDigit(aNum, aDigit) set aStr to aNum as string set aLen to length of aStr if aLen < aDigit then return false –エラー end if set tStr to character (aLen – aDigit + 1) of aStr return tStr as integer end getDigit –指定数値のうち指定桁の数字を返す on setDigit(aNum, aDigit, newNum) set aStr to aNum as string set aLen to length of aStr if aLen < aDigit then return false –エラー end if set aList to characters of aStr set item (aLen – aDigit + 1) of aList to (newNum as string) set aaStr to aList as string return aaStr as integer end setDigit |
Macの製品カテゴリ名を取得する
AppleScript名:Macの製品カテゴリ名を取得する |
— Created 2015-11-01 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set macRes to getSystemProfileInAGenre("SPHardwareDataType", "machine_name") of me –> "MacBook Pro" on getSystemProfileInAGenre(aTargGenre as string, aTargKey as string) set sRes to do shell script ("/usr/sbin/system_profiler -xml " & aTargGenre) set aSource to (readPlistFromStr(sRes) of me) as list set aaList to contents of first item of aSource set aList to _items of aaList repeat with i in aList set aDict to (current application’s NSMutableDictionary’s dictionaryWithDictionary:(contents of i)) set aKeyList to (aDict’s allKeys()) as list if aTargKey is in aKeyList then set aRes to (aDict’s valueForKeyPath:aTargKey) if aRes is not equal to missing value then return aRes as string end if end if end repeat return false end getSystemProfileInAGenre –stringのplistを読み込んでRecordに on readPlistFromStr(theString) set aSource to current application’s NSString’s stringWithString:theString set pListData to aSource’s dataUsingEncoding:(current application’s NSUTF8StringEncoding) set aPlist to current application’s NSPropertyListSerialization’s propertyListFromData:pListData mutabilityOption:(current application’s NSPropertyListImmutable) |format|:(current application’s NSPropertyListFormat) errorDescription:(missing value) return aPlist end readPlistFromStr |
ソフトウェア的にアンマウントしたが物理的に接続解除していないHard Driveの名前を取得する v2
ソフトウェア的にアンマウントした状態で、各種接続端子(USBとか)で物理的につながったままの外部ドライブの名称を取得するAppleScriptです。
いちど、そういう質問が来て「できるわけがない」という回答をしていました。AppleScriptの性質上、ソフトウェア的にそういう操作は許可されていません。そもそも、そんな機能はありません。
ただ、まさかshell command経由でそうした操作ができるとは知りませんでした。冗談半分で調べてみたら存在したという状況です。
AppleScript名:ソフトウェア的にアンマウントしたが物理的に接続解除していないHard Driveの名前を取得する v2 |
— Created 2017-06-20 Shane Stanley — Modified 2017-06-20 Takaaki Naganoya use AppleScript version "2.4" — Yosemite (10.10) or later use framework "Foundation" use scripting additions set theResult to do shell script "diskutil list -plist" — make dictionary from property list set theResult to current application’s NSString’s stringWithString:theResult set pListDict to theResult’s propertyList() –> (NSDictionary) {AllDisks:{"disk0", "disk0s1", "disk0s2", "disk0s3", "disk1"}, AllDisksAndPartitions:{{DeviceIdentifier:"disk0", Size:500277790720, Content:"GUID_partition_scheme", Partitions:{{DiskUUID:"FC89778D-9028-4FAA-8118-F74802B0664E", DeviceIdentifier:"disk0s1", Size:209715200, Content:"EFI", VolumeName:"EFI", VolumeUUID:"85D67001-D93E-3687-A1C2-79D677F0C2E0"}, {DiskUUID:"AAF54C8C-9229-4DC1-8EA5-5FCCE9050DC4", DeviceIdentifier:"disk0s2", Size:499418034176, Content:"Apple_CoreStorage"}, {DiskUUID:"EE72FC16-C614-4C77-9048-6EF118451D48", DeviceIdentifier:"disk0s3", Size:650002432, Content:"Apple_Boot", VolumeName:"Recovery HD", VolumeUUID:"3400E36B-24AA-3B7A-93E3-BBDCDFF48548"}}}, {DeviceIdentifier:"disk1", Size:499055067136, MountPoint:"/", Content:"Apple_HFS", VolumeName:"Cherry"}}, WholeDisks:{"disk0", "disk1"}, VolumesFromDisks:{"Cherry"}} — extract relevant info set disksAndParitions to pListDict’s objectForKey:"AllDisksAndPartitions" –> (NSArray) {{DeviceIdentifier:"disk0", Size:500277790720, Content:"GUID_partition_scheme", Partitions:{{DiskUUID:"FC89778D-9028-4FAA-8118-F74802B0664E", DeviceIdentifier:"disk0s1", Size:209715200, Content:"EFI", VolumeName:"EFI", VolumeUUID:"85D67001-D93E-3687-A1C2-79D677F0C2E0"}, {DiskUUID:"AAF54C8C-9229-4DC1-8EA5-5FCCE9050DC4", DeviceIdentifier:"disk0s2", Size:499418034176, Content:"Apple_CoreStorage"}, {DiskUUID:"EE72FC16-C614-4C77-9048-6EF118451D48", DeviceIdentifier:"disk0s3", Size:650002432, Content:"Apple_Boot", VolumeName:"Recovery HD", VolumeUUID:"3400E36B-24AA-3B7A-93E3-BBDCDFF48548"}}}, {DeviceIdentifier:"disk1", Size:499055067136, MountPoint:"/", Content:"Apple_HFS", VolumeName:"Cherry"}} set partitionsArray to current application’s NSMutableArray’s array() — to store values repeat with anEntry in disksAndParitions set thePartitions to (anEntry’s objectForKey:"Partitions") if thePartitions = missing value then — no partitions means a volume (partitionsArray’s addObject:anEntry) else (partitionsArray’s addObjectsFromArray:thePartitions) end if end repeat — filter by Content type set thePred to current application’s NSPredicate’s predicateWithFormat:"Content == ’Apple_HFS’ OR Content == ’Apple_APFS’ " set partitionsArray to partitionsArray’s filteredArrayUsingPredicate:thePred — get names return (partitionsArray’s valueForKey:"VolumeName") as list |
指定のDiskの情報を取得する
AppleScript名:指定のDiskの情報を取得する |
— Created 2016-04-06 by Takaaki Naganoya — 2016 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set targDev to "/dev/disk3" –Blu-ray drive via USB set aDict to current application’s NSMutableDictionary’s alloc()’s init() try set a to do shell script "diskutil info " & targDev on error return false end try set aList to paragraphs of a repeat with i in aList set j to contents of i if j is not equal to "" then set jj to parseByDelim(j, ":") of me if (count every item of jj) is equal to 2 then copy jj to {j2, j3} set jj2 to (current application’s NSString’s stringWithString:j2) set jj3 to (current application’s NSString’s stringWithString:j3) set jjj2 to (jj2’s stringByTrimmingCharactersInSet:(current application’s NSCharacterSet’s whitespaceCharacterSet())) set jjj3 to (jj3’s stringByTrimmingCharactersInSet:(current application’s NSCharacterSet’s whitespaceCharacterSet())) (aDict’s setValue:jjj3 forKey:jjj2) end if end if end repeat return aDict as list of string or string –> {Read-Only Media:"Yes", OS Can Be Installed:"No", Volume Name:"Not applicable (no file system)", OS 9 Drivers:"No", Virtual:"No", Device Block Size:"2048 Bytes", Low Level Format:"Not supported", Removable Media:"Yes", Device / Media Name:"MATSHITA BD-MLT UJ240AS", Optical Media Erasable:"No", Optical Drive Type:"CD-ROM, CD-R, CD-RW, DVD-ROM, DVD-R, DVD-R DL, DVD-RW, DVD-RAM, DVD+R, DVD+R DL, DVD+RW, BD-ROM, BD-R, BD-RE", Part of Whole:"disk3", Device Identifier:"disk3", Whole:"Yes", Optical Media Type:"BD-R", SMART Status:"Not Supported", Device Location:"External", Volume Free Space:"Not applicable (no file system)", Device Node:"/dev/disk3", Read-Only Volume:"Not applicable (no file system)", Media Type:"Generic", Total Size:"0 B (0 Bytes) (exactly 0 512-Byte-Units)", Media Removal:"Software-Activated", Mounted:"Not applicable (no file system)", Content (IOContent):"None", File System:"None", Protocol:"USB"} –テキストを指定デリミタでリスト化 on parseByDelim(aData, aDelim) set aText to current application’s NSString’s stringWithString:aData set aList to aText’s componentsSeparatedByString:aDelim return aList as list end parseByDelim |
Input Methodのメニュー表示名を取得する
AppleScript名:Input Methodのメニュー表示名を取得する |
— Created 2017-01-22 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "InputManager" –https://github.com/jensnockert/input-manager set cList to (current application’s CSInputSource’s all()’s valueForKey:"localizedName") as list –> {"ひらがな", "英字", "カタカナ", "日本語", "かなパレット", "com.apple.PressAndHold", "絵文字と記号", "キーボードビューア", "EmojiFunctionRowIM_Extension"}–10.10.x –> {"ひらがな", "英字", "カタカナ", "日本語", "かなパレット", "com.apple.PressAndHold", "絵文字と記号", "キーボードビューア"}–10.12.x |
InputManagerでIMを切り換える
InputManager.frameworkを呼び出して、日本語入力Input Methodの入力文字の切り替えを行うAppleScriptです。
スクリプトエディタ上でControl-Command-Rにてフロントエンド・プロセスで実行する必要があります。
AppleScript名:InputManagerでIMを切り換える |
— Created 2017-01-22 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" use framework "InputManager" –https://github.com/jensnockert/input-manager –Check If this script runs in foreground if not (current application’s NSThread’s isMainThread()) as boolean then display alert "This script must be run from the main thread (Command-Control-R in Script Editor)." buttons {"Cancel"} as critical error number -128 end if –Caution: This script runs on only Japanese language environment set cList to current application’s CSInputSource’s all() set dList to (cList’s valueForKey:"localizedName") set aRes to ((dList’s indexOfObject:"ひらがな") as integer) –"Hiragana" in Japanese set bRes to ((dList’s indexOfObject:"英字") as integer) –"English Letters" in Japanese set hiraKey to cList’s objectAtIndex:aRes set engKey to cList’s objectAtIndex:bRes repeat 10 times hiraKey’s |select|() delay 1 engKey’s |select|() delay 1 end repeat |