AS関連データの取り扱いを簡単にすることを目的に書き出した、privateDatatypeLibです。
プログラムとデータを分離して、データ記述部分を外部ファイル(設定ファイルなど)に追い出したときに、どのようなデータかを表現するための道具として試作してみました。
macOS上のデータ記述子は、
などのさまざまなデータ識別方法が存在していますが、どれも(自分の)用途には合わなかったので、検討・試作をはじめてみました。Predicatesが一番近いものの、不十分なのでいろんなデータ型や用途に拡張。あくまで自分用なので「public」などの宣言はとくに不必要と考え、縮小して処理を行なっています。
とくに、ファイルパスの処理なんて定型処理しかしないのに、わざわざ何かの表現を行う必要があるのはナンセンスですし、日付関連も割と余計な記述が多いように感じています。
また、緯度/経度のデータや座標データなども、もう少しなんとかならないかと思っています。
AppleScript名:privateDataTypeLib.scptd |
— – Created by: Takaaki Naganoya – Created on: 2022/08/22 — – Copyright © 2022 Piyomaru Software, All Rights Reserved — use AppleScript version "2.4" — Yosemite (10.10) or later use framework "Foundation" use scripting additions set aData to "100" set dType to "@number" set bData to "" –不要なケース多数。不要な場合にはヌル文字を指定 set mePath to path to me –実際には、呼び出し側で取得して指定。ライブラリ自体のパスを求めているのはテスト実行時のみ set aRes to getParameterAndCalc(aData, dType, bData, mePath) of me on getParameterAndCalc(aData, dType, bData, mePath) if dType = "@string" then return normalizeByNFC(aData) of me else if dType = "@number" then set tmpA to zenToHan(aData) of charConvKit of me –全角→半角変換 set a to detectOutNumStr(tmpA) of me –数字+関連・意外の文字を除外 return normalizeByNFC(a) of me else if dType = "@filepath.sub.foldername" then set aClass to class of aData –aliasだったらPOSIX pathに変換。file…はどうなんだか if aClass = alias then set aData to POSIX path of aData return aData & bData & "/" else if dType = "@file.comment" then set aStr to getFinderComment(POSIX path of mePath) of me return normalizeByNFC(aStr) of me else if dType = "@file.name" then set aClass to class of aData if aClass = alias then set aData to POSIX path of aData set aStr to (current application’s NSString’s stringWithString:aData)’s lastPathComponent()’s stringByDeletingPathExtension() return normalizeByNFC(aStr as string) of me else if dType = "@date.month" then set curDate to current date set curMonth to month of curDate as number return curMonth as string else return aData end if end getParameterAndCalc on normalizeByNFC(aStr) set aNSStr to current application’s NSString’s stringWithString:aStr set aNFC to aNSStr’s precomposedStringWithCanonicalMapping() return aNFC as string end normalizeByNFC –ANK文字列以外のものをそぎ落とす on detectOutNumStr(testStr) set sList to characters of testStr set aStr to "" repeat with i in sList if detectOutNumChar(i) of me then set aStr to aStr & (i as string) end if end repeat return aStr end detectOutNumStr on detectOutNumChar(testText) –Numeric + Special char set ankChar to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ".", "-", "+", "E", ","} set _testChar to testText as Unicode text ignoring case repeat with i in _testChar set j to contents of i if j is not in ankChar then return false end if end repeat end ignoring return true end detectOutNumChar –Finderコメントを取得 on getFinderComment(aPOSIX) set aURL to current application’s |NSURL|’s fileURLWithPath:aPOSIX set aMetaInfo to current application’s NSMetadataItem’s alloc()’s initWithURL:aURL set metaDict to (aMetaInfo’s valuesForAttributes:{"kMDItemFinderComment"}) as record if metaDict = {} then return "" set aComment to kMDItemFinderComment of (metaDict) return aComment end getFinderComment script charConvKit — Created 2017-09-06 by Shane Stanley — Modified 2017-09-06 by Takaaki Naganoya use AppleScript use framework "Foundation" property parent : AppleScript property NSString : a reference to current application’s NSString property NSStringTransformFullwidthToHalfwidth : a reference to current application’s NSStringTransformFullwidthToHalfwidth property NSStringTransformHiraganaToKatakana : a reference to current application’s NSStringTransformHiraganaToKatakana property NSStringTransformLatinToHiragana : a reference to current application’s NSStringTransformLatinToHiragana property NSStringTransformLatinToKatakana : a reference to current application’s NSStringTransformLatinToKatakana property NSStringTransformToUnicodeName : a reference to current application’s NSStringTransformToUnicodeName property NSStringTransformToXMLHex : a reference to current application’s NSStringTransformToXMLHex –半角→全角変換 on hanToZen(aStr) set aString to NSString’s stringWithString:aStr return (aString’s stringByApplyingTransform:(NSStringTransformFullwidthToHalfwidth) |reverse|:true) as string end hanToZen –全角→半角変換 on zenToHan(aStr) set aString to NSString’s stringWithString:aStr return (aString’s stringByApplyingTransform:(NSStringTransformFullwidthToHalfwidth) |reverse|:false) as string end zenToHan –ひらがな→カタカナ変換 on hiraganaToKatakana(aStr) set aString to NSString’s stringWithString:aStr return (aString’s stringByApplyingTransform:(NSStringTransformHiraganaToKatakana) |reverse|:false) as string end hiraganaToKatakana –カタカナ→ひらがな変換 on katakanaToHiraganaTo(aStr) set aString to NSString’s stringWithString:aStr return (aString’s stringByApplyingTransform:(NSStringTransformHiraganaToKatakana) |reverse|:true) as string end katakanaToHiraganaTo –ローマ字→ひらがな変換 on alphabetToHiragana(aStr) set aString to NSString’s stringWithString:aStr return (aString’s stringByApplyingTransform:(NSStringTransformLatinToHiragana) |reverse|:false) as string end alphabetToHiragana –ひらがな→ローマ字変換 on hiraganaToalphabet(aStr) set aString to NSString’s stringWithString:aStr return (aString’s stringByApplyingTransform:(NSStringTransformLatinToHiragana) |reverse|:true) as string end hiraganaToalphabet –ローマ字→カタカナ変換 on alphabetToKatakana(aStr) set aString to NSString’s stringWithString:aStr return (aString’s stringByApplyingTransform:(NSStringTransformLatinToKatakana) |reverse|:false) as string end alphabetToKatakana –カタカナ→ローマ字変換 on katakanaToAlphabet(aStr) set aString to NSString’s stringWithString:aStr return (aString’s stringByApplyingTransform:(NSStringTransformLatinToKatakana) |reverse|:true) as string end katakanaToAlphabet –文字→Unicode Name変換 on characterToUnicodeName(aStr) set aString to NSString’s stringWithString:aStr return (aString’s stringByApplyingTransform:(NSStringTransformToUnicodeName) |reverse|:false) as string end characterToUnicodeName –Unicode Name→文字変換 on unicodeNameToCharacter(aStr) set aString to NSString’s stringWithString:aStr return (aString’s stringByApplyingTransform:(NSStringTransformToUnicodeName) |reverse|:true) as string end unicodeNameToCharacter –文字→XML Hex変換 on stringToXMLHex(aStr) set aString to NSString’s stringWithString:aStr return (aString’s stringByApplyingTransform:(NSStringTransformToXMLHex) |reverse|:false) as string end stringToXMLHex –XML Hex→文字変換 on xmlHexTostring(aStr) set aString to NSString’s stringWithString:aStr return (aString’s stringByApplyingTransform:(NSStringTransformToXMLHex) |reverse|:true) as string end xmlHexTostring end script |