Menu

Skip to content
AppleScriptの穴
  • Home
  • Products
  • Books
  • Docs
  • Events
  • Forum
  • About This Blog
  • License
  • 仕事依頼

AppleScriptの穴

Useful & Practical AppleScript archive. Click '★Click Here to Open This Script' Link to download each AppleScript

月: 2019年11月

指定アプリケーションの指定ロケールのフォルダ内の該当キーワードを含むstringsファイル情報を抽出する

Posted on 11月 13, 2019 by Takaaki Naganoya

Bundle IDで指定したアプリケーションからすべてのLocaleを取得し、ダイアログ選択したLocale内のすべてのstringsファイルを読み込んでNSDictionary化して指定のキーワードを含むものを抽出するAppleScriptです。

指定キーワードが含まれていた場合には、

キー, 値, stringsファイルのパス

をリストで返します。調査のための下調べを行うものです。

AppleScript名:指定アプリケーションの指定ロケールのフォルダ内の該当キーワードを含むstringsファイル情報を抽出する
—
–  Created by: Takaaki Naganoya
–  Created on: 2019/09/21
—
–  Copyright © 2019 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"

property |NSURL| : a reference to current application’s |NSURL|
property NSArray : a reference to current application’s NSArray
property NSBundle : a reference to current application’s NSBundle
property NSPredicate : a reference to current application’s NSPredicate
property NSDictionary : a reference to current application’s NSDictionary
property NSWorkspace : a reference to current application’s NSWorkspace
property NSFileManager : a reference to current application’s NSFileManager
property NSMutableDictionary : a reference to current application’s NSMutableDictionary
property NSURLTypeIdentifierKey : a reference to current application’s NSURLTypeIdentifierKey
property NSDirectoryEnumerationSkipsHiddenFiles : a reference to current application’s NSDirectoryEnumerationSkipsHiddenFiles
property NSDirectoryEnumerationSkipsPackageDescendants : a reference to current application’s NSDirectoryEnumerationSkipsPackageDescendants

set targID to "com.apple.Finder"
set targWords to "のコピー"

set bRes to getLocalizationsFromBundleID(targID) of me
–>  {"de", "he", "en_AU", "ar", "el", "ja", "en", "uk", "es_419", "zh_CN", "es", "da", "it", "sk", "pt_PT", "ms", "sv", "cs", "ko", "Base", "no", "hu", "zh_HK", "tr", "pl", "zh_TW", "en_GB", "vi", "ru", "fr_CA", "fr", "fi", "id", "nl", "th", "pt", "ro", "hr", "hi", "ca"}

–Select Localization
set curLang to first item of (choose from list bRes)

–Get All strings file path within a bundle
set aPath to retPathFromBundleID(targID) of me
set aPath to aPath & "/Contents/Resources/" & curLang & ".lproj"
set sList to getFilepathListByUTI(aPath, "com.apple.xcode.strings-text", "POSIX") of me

–Get Every Key & Value pair then filter target keyword
set aMDict to NSMutableDictionary’s new()
set hitList to {}
repeat with ii in sList
  set jj to contents of ii
  
set aDict to (NSDictionary’s alloc()’s initWithContentsOfFile:jj)
  (
aMDict’s addEntriesFromDictionary:aDict)
  
  
set kList to aMDict’s allKeys() as list
  
set vList to aMDict’s allValues() as list
  
  
set kCount to 1
  
repeat with i in vList
    set j to contents of i
    
if j contains targWords then
      set the end of hitList to {contents of item kCount of kList, j, jj}
    end if
    
set kCount to kCount + 1
  end repeat
  
end repeat

return hitList
–> {{"PW5_V2", "^0項目のコピーの準備中", "/System/Library/CoreServices/Finder.app/Contents/Resources/Japanese.lproj/Localizable.strings"}, …}

on getLocalizationsFromBundleID(aBundleID)
  set aRes to retPathFromBundleID(aBundleID) of me
  
if aRes = false then error "Wrong Bundle ID."
  
return getSpecifiedAppFilesLocalizationListWithDuplication(aRes) of me
end getLocalizationsFromBundleID

–指定アプリケーションファイルの、指定Localeにおけるローカライズ言語リストを求める。重複を許容
on getSpecifiedAppFilesLocalizationListWithDuplication(appPOSIXpath)
  set aURL to (|NSURL|’s fileURLWithPath:appPOSIXpath)
  
set aBundle to NSBundle’s bundleWithURL:aURL
  
set locList to aBundle’s localizations()
  
return locList as list
end getSpecifiedAppFilesLocalizationListWithDuplication

on retPathFromBundleID(aBundleID)
  set aURL to NSWorkspace’s sharedWorkspace()’s URLForApplicationWithBundleIdentifier:aBundleID
  
if aURL = missing value then return false –Error
  
return aURL’s |path|() as string
end retPathFromBundleID

on getFilepathListByUTI(aFolPOSIX, aUTI as string, aFileType as string)
  script spdFile
    property urlList : {}
  end script
  
  
if aFileType is not in {"file", "POSIX"} then return {}
  
  
set aFM to NSFileManager’s defaultManager()
  
set aFolExt to (aFM’s fileExistsAtPath:aFolPOSIX isDirectory:true) as boolean
  
if aFolExt = false then return {} –フォルダ自体が存在しなければヌルリストを返す
  
  
set aURL to |NSURL|’s fileURLWithPath:aFolPOSIX
  
set theOptions to ((NSDirectoryEnumerationSkipsPackageDescendants) as integer) + ((NSDirectoryEnumerationSkipsHiddenFiles) as integer)
  
set urlArray to (aFM’s contentsOfDirectoryAtURL:aURL includingPropertiesForKeys:{} options:theOptions |error|:(missing value))
  
if urlArray = missing value then return {}
  
  
set (urlList of spdFile) to urlArray as list
  
set newList to {}
  
  
repeat with i in (urlList of spdFile)
    set j to POSIX path of i
    
set tmpUTI to my retUTIfromPath(j)
    
set utiRes to my filterUTIList({tmpUTI}, aUTI)
    
    
if utiRes is not equal to {} then
      if aFileType = "POSIX" then
        set the end of newList to j
      else if aFileType = "file" then
        set the end of newList to POSIX file j
      end if
    end if
    
  end repeat
  
  
return newList
end getFilepathListByUTI

–指定のPOSIX pathのファイルのUTIを求める
on retUTIfromPath(aPOSIXPath)
  set aURL to |NSURL|’s fileURLWithPath:aPOSIXPath
  
set {theResult, theValue} to aURL’s getResourceValue:(reference) forKey:NSURLTypeIdentifierKey |error|:(missing value)
  
  
if theResult = true then
    return theValue as string
  else
    return theResult
  end if
end retUTIfromPath

–UTIリストが指定UTIに含まれているかどうか演算を行う
on filterUTIList(aUTIList, aUTIstr)
  set anArray to NSArray’s arrayWithArray:aUTIList
  
set aPred to NSPredicate’s predicateWithFormat_("SELF UTI-CONFORMS-TO %@", aUTIstr)
  
set bRes to (anArray’s filteredArrayUsingPredicate:aPred) as list
  
return bRes
end filterUTIList

★Click Here to Open This Script 

Posted in file File path UTI | Tagged 10.12savvy 10.13savvy 10.14savvy 10.15savvy Finder NSArray NSBundle NSDictionary NSDirectoryEnumerationSkipsHiddenFiles NSDirectoryEnumerationSkipsPackageDescendants NSFileManager NSMutableDictionary NSPredicate NSURL NSURLTypeIdentifierKey NSWorkspace | Leave a comment

指定アプリケーション内の全Localeの指定stringsファイル内の指定キーの値を取得する

Posted on 11月 13, 2019 by Takaaki Naganoya

Bundle IDで指定したアプリケーションのバンドル中(/Contents/Resources/)のすべてのLocalizationに存在する指定のstringsファイル内で、指定キーの値を取得するAppleScriptです。

macOSの日本語環境では、Finder上でコピーしたファイルのファイル名の後方に「のコピー」といった文字列が追記されますが、他のLocaleでどのような文字列が使用されているか調査するために作成したものです。

AppleScript名:指定アプリケーション内の全Localeの指定stringsファイル内の指定キーの値を取得する.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2019/11/12
—
–  Copyright © 2019 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"

property |NSURL| : a reference to current application’s |NSURL|
property NSArray : a reference to current application’s NSArray
property NSBundle : a reference to current application’s NSBundle
property NSPredicate : a reference to current application’s NSPredicate
property NSDictionary : a reference to current application’s NSDictionary
property NSWorkspace : a reference to current application’s NSWorkspace
property NSFileManager : a reference to current application’s NSFileManager
property NSMutableDictionary : a reference to current application’s NSMutableDictionary
property NSURLTypeIdentifierKey : a reference to current application’s NSURLTypeIdentifierKey

set targID to "com.apple.Finder"
set targFile to "Localizable.strings"
set targKey to "N4_V2"
set vList to getAValInASpecifiedStringFileOfAllLocaleInABundle(targID, targFile, targKey) of me
–> {{"he", "עותק של ^=1 ^=0"}, {"en_AU", "^=1 copy ^=0"}, {"ar", "نسخة ^=0 من ^=1"}, {"el", "^=1 αντίγραφο ^=0"}, {"uk", "^=1 копія ^=0"}, {"English", "^=1 copy ^=0"}, {"es_419", "Copia de ^=1 ^=0"}, {"zh_CN", "^=1的副本 ^=0"}, {"Dutch", "^=1 kopie ^=0"}, {"da", "^=1-kopi ^=0"}, {"sk", "^=1 – kópia ^=0"}, {"pt_PT", "^=1 – cópia ^=0"}, {"German", "^=1 Kopie ^=0"}, {"ms", "salinan ^=0 ^=1"}, {"sv", "^=1 (kopia ^=0)"}, {"cs", "^=1 (kopie ^=0)"}, {"ko", "^=1 복사본 ^=0"}, {"no", "^=1-kopi ^=0"}, {"hu", "^=1 másolat ^=0"}, {"zh_HK", "^=1 副本 ^=0"}, {"Spanish", "^=1 copia ^=0"}, {"tr", "^=1 kopyası ^=0"}, {"pl", "^=1-kopia ^=0"}, {"zh_TW", "^=1 拷貝 ^=0"}, {"en_GB", "^=1 copy ^=0"}, {"French", "^=1 copie ^=0"}, {"vi", "Bản sao ^=1 ^=0"}, {"ru", "^=1 — копия ^=0"}, {"fr_CA", "^=1 copie ^=0"}, {"fi", "^=1 kopio ^=0"}, {"id", "Salinan ^=1 ^=0"}, {"th", "^=1 สำเนา ^=0"}, {"pt", "^=1 – cópia ^=0"}, {"ro", "^=1 – copie ^=0"}, {"Japanese", "^=1のコピー^=0"}, {"hr", "^=1 kopija ^=0"}, {"hi", "^=1 कॉपी ^=0"}, {"Italian", "^=1 copia ^=0"}, {"ca", "^=1 còpia ^=0"}}

on getAValInASpecifiedStringFileOfAllLocaleInABundle(targID, targFile, targKey)
  –Get App Path
  
set aPath to retPathFromBundleID(targID) of me
  
  
–Get all Localizations
  
set bRes to getLocalizationsFromBundleID(targID) of me
  
  
set hitList to {}
  
  
–Loop with Localizations in an application bundle
  
repeat with iii in bRes
    set jjj to contents of iii
    
set allPath to aPath & "/Contents/Resources/" & jjj & ".lproj/" & targFile
    
set aDict to (NSDictionary’s alloc()’s initWithContentsOfFile:allPath)
    
if aDict is not equal to missing value then
      set aVal to (aDict’s valueForKeyPath:(targKey))
      
if aVal is not equal to missing value then
        set the end of hitList to {jjj, aVal as string}
      end if
    end if
  end repeat
  
  
return hitList
end getAValInASpecifiedStringFileOfAllLocaleInABundle

on getLocalizationsFromBundleID(aBundleID)
  set aRes to retPathFromBundleID(aBundleID) of me
  
if aRes = false then error "Wrong Bundle ID."
  
return getSpecifiedAppFilesLocalizationListWithDuplication(aRes) of me
end getLocalizationsFromBundleID

–指定アプリケーションファイルの、指定Localeにおけるローカライズ言語リストを求める。重複を許容
on getSpecifiedAppFilesLocalizationListWithDuplication(appPOSIXpath)
  set aURL to (|NSURL|’s fileURLWithPath:appPOSIXpath)
  
set aBundle to NSBundle’s bundleWithURL:aURL
  
set locList to aBundle’s localizations()
  
return locList as list
end getSpecifiedAppFilesLocalizationListWithDuplication

on retPathFromBundleID(aBundleID)
  set aURL to NSWorkspace’s sharedWorkspace()’s URLForApplicationWithBundleIdentifier:aBundleID
  
if aURL = missing value then return false –Error
  
return aURL’s |path|() as string
end retPathFromBundleID

★Click Here to Open This Script 

Posted in System Text | Tagged 10.12savvy 10.13savvy 10.14savvy 10.15savvy Finder NSArray NSBundle NSDictionary NSFileManager NSMutableDictionary NSPredicate NSURL NSURLTypeIdentifierKey NSWorkspace | Leave a comment

数値に3桁セパレータを付加、外して数値に戻す v2

Posted on 11月 12, 2019 by Takaaki Naganoya

10進数の数字文字列に3桁セパレータを付加、外して数字文字列に戻すAppleScriptです。

めんどくさい上にCocoaに機能があり、かつそれほど大量のデータ処理を行うわけでもない(スピードを要求されない)処理のはずなので、手抜きでCocoaの機能を呼び出しています。

AppleScript名:数値に3桁セパレータを付加、外して数値に戻す v2
— Created 2016-10-12 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
–3桁セパレータ文字「,」をNSLocaleのcurrentLocaleから取得するように変更

property NSString : a reference to current application’s NSString
property NSLocale : a reference to current application’s NSLocale
property NSCharacterSet : a reference to current application’s NSCharacterSet
property NSNumberFormatter : a reference to current application’s NSNumberFormatter
property NSLocaleGroupingSeparator : a reference to current application’s NSLocaleGroupingSeparator
property NSNumberFormatterDecimalStyle : a reference to current application’s NSNumberFormatterDecimalStyle

set aNum to 100000000
set aStr to formatNum(aNum) of me
–> "100,000,000"

set bNum to deFromatNumStr(aStr) of me
–>  "100000000"

on formatNum(theNumber as number)
  set theResult to NSNumberFormatter’s localizedStringFromNumber:theNumber numberStyle:(NSNumberFormatterDecimalStyle)
  
return theResult as text
end formatNum

on deFromatNumStrAndRetNumber(theNumericString as string)
  set aThousandSep to NSLocale’s currentLocale()’s objectForKey:(NSLocaleGroupingSeparator)
  
set notWantChars to NSCharacterSet’s characterSetWithCharactersInString:aThousandSep
  
set targStr to NSString’s stringWithString:theNumericString
  
set newStr to (targStr’s componentsSeparatedByCharactersInSet:notWantChars)’s componentsJoinedByString:""
  
return ((newStr as string) as number) –Danger in OS X 10.10 (floating point casting bug)
end deFromatNumStrAndRetNumber

on deFromatNumStr(theNumericString as string)
  set aThousandSep to NSLocale’s currentLocale()’s objectForKey:(NSLocaleGroupingSeparator)
  
set notWantChars to NSCharacterSet’s characterSetWithCharactersInString:aThousandSep
  
set targStr to NSString’s stringWithString:theNumericString
  
set newStr to (targStr’s componentsSeparatedByCharactersInSet:notWantChars)’s componentsJoinedByString:""
  
return (newStr as string)
end deFromatNumStr

★Click Here to Open This Script 

Posted in Number Text | Tagged 10.12savvy 10.13savvy 10.14savvy 10.15savvy NSCharacterSet NSLocale NSLocaleGroupingSeparator NSNumberFormatter NSNumberFormatterDecimalStyle NSString | Leave a comment

10進数とn進数文字列の相互変換

Posted on 11月 12, 2019 by Takaaki Naganoya

10進数の数値とn進数の文字列を相互変換するAppleScriptです。

もともと、AppleScriptの言語仕様上、10進数とn進数文字列との相互変換機能は存在しないのですが、サードパーティから機能拡張書類(OSAX)でそのような機能が提供され、さらにClassic MacOSからMac OS Xへの移行時にそれらの機能拡張書類が使えなくなって(一部は移行)、個別にそれらの機能をスクリプター側で実装してMailing ListやBBS上、Blogなどで共有してきたという経緯があります。

AppleScript Users MLに流れていたもののような気がしますが、自分で作ったものかもしれません。出どころ不明です。

ただ、2進数変換や16進数変換などの各種変換ルーチンが乱立していたところに、「これ、全部1つで済むんじゃね?」と思ってまとめたような気がしないでもありません。自分で作っても簡単なので。

Cocoaの機能を一切使っていません。たしかにCocoaの機能を用いての変換も行えるのですが、データのサイズがとても小さい(桁数が少ない)ので、Cocoaの機能を呼び出さないほうが高速です。

もしも、AppleScriptの予約語のようにこれらの機能を呼び出したい場合には、これらのScriptをAppleScript Librariesにして、AppleScript用語辞書をつけて書き出せば(辞書はなくても大丈夫ですが)、

use nthLib: script "nThConvLib"

のように呼び出すことができるようになっています(macOS 10.9以降)。

AppleScript名:10進数をn進数文字列に変換する
set a to 100

–10進→16進数変換
set b to aNumToNthDecimal(a, 16, 4, {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}, true) of me
–> "64"

–10進→8進数変換
set c to aNumToNthDecimal(a, 8, 4, {"0", "1", "2", "3", "4", "5", "6", "7"}, true) of me
–> "144"

–10進→2進数変換
set d to aNumToNthDecimal(a, 2, 8, {"0", "1"}, false) of me
–> "01100100"

–10進数をn進数文字列に変換する

–origNum:変換対象の数字
–nTh: n進数のn
–stringLength: 出力指定桁(上位桁のアキはゼロパディングされる)
–zeroSuppress: 上位桁がゼロになった場合に評価を打ち切るかのフラグ、ゼロパディングを行わない(ゼロサプレスする)場合に指定
on aNumToNthDecimal(origNum, nTh, stringLength, stringSetList, zeroSuppress)
  set resString to {}
  
repeat with i from stringLength to 1 by -1
    if {origNum, zeroSuppress} = {0, true} then exit repeat
    
set resNum to (origNum mod nTh)
    
set resText to contents of item (resNum + 1) of stringSetList
    
set resString to resText & resString
    
set origNum to origNum div nTh
  end repeat
  
return (resString as string)
end aNumToNthDecimal

★Click Here to Open This Script 

AppleScript名:n進数文字列を10進数に変換する v2a
–2進数文字列→10進数数値変換
set a to "11"
set b to aNthToDecimal(a, {"0", "1"}) of me
–> 3

–8進数文字列→10進数数値変換
set a to "144"
set c to aNthToDecimal(a, {"0", "1", "2", "3", "4", "5", "6", "7"}) of me
–> 100

–16進数文字列→10進数数値変換
set a to "64"
set b to aNthToDecimal(a, {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}) of me
–> 100

–n進数文字列を10進数に変換する
on aNthToDecimal(origStr as string, nTh as list)
  set resNumber to 0
  
set sList to reverse of (characters of origStr)
  
set aLen to length of nTh
  
set digitCount to 0
  
  
repeat with i in sList
    set j to contents of i
    
set aRes to (offsetInList(j, nTh) of me)
    
    
set resNumber to resNumber + (aLen ^ digitCount) * (aRes – 1)
    
set digitCount to digitCount + 1
  end repeat
  
  
return resNumber as integer
end aNthToDecimal

–元はCocoaの機能を利用していたが、処理対象のデータが小さいのでOld Style AppleScriptで書き直した
on offsetInList(aChar as string, aList as list)
  set aCount to 1
  
repeat with i in aList
    set j to contents of i
    
if aChar is equal to j then
      return aCount
    end if
    
set aCount to aCount + 1
  end repeat
  
return 0
end offsetInList

★Click Here to Open This Script 

Posted in Number Text | Tagged 10.12savvy 10.13savvy 10.14savvy 10.15savvy | Leave a comment

macOS 10.15のPDFViewのバグに対処した簡易PDFリーダー

Posted on 11月 7, 2019 by Takaaki Naganoya

従来のmacOSではPDFリーダー「Skim」上で「applescript://」スキームのURLリンクをクリックした場合に正常にスクリプトエディタ/Script Debuggerに新規スクリプト作成のURLイベントが伝達されていました。

macOS 10.15.xには、PDFView上でPDF閲覧中にクリックしたURLが「applescript://」プロトコルの場合に、正確にデコードされるべき内容がデコードされずにスクリプトエディタに転送されるというバグが確認されています。

このmacOS 10.15のPDFViewのバグに対処するための簡易PDFリーダーのアプリケーション「PiyoREADER」を作成してみました。フリーで配布いたします。コード署名はしてありますが、ダウンロードして初回起動時にハネられるので、システム環境設定の「セキュリティとプライバシー」>「一般」で「そのまま起動」を選ぶ必要があります。

–> Download PiyoReader(2.1MB)

この簡易PDFリーダーにはもう1つ重要な機能があります。PDF中の「http://」スキームのURLリンククリック時に、本BlogのURLである場合には内部テーブルにもとづきURLをフォワードするという機能です。つまり、2018年1月末のXserver.jpによるBlog消去以前のURLを、現行のBlog上のURLに変換してオープンするというものです。

なお、PDF内に仕込まれた「applescript://」スキームのURLリンクはスクリプトエディタに、「http://」スキームのURLリンクはSafariに伝えるように固定しています。

PiyoReaderはmacOS 10.10以降で動作し、macOS 10.15以外のまともに「applescript://」スキームのURLを扱えるOSでは、「AppleScriptの穴」BlogのURL変換の機能が活用できる簡易PDFリーダーということになります。

Piyomaru Softwareが執筆・刊行している各種電子書籍には「applescript://」URLリンクが埋め込んであり、ご好評をいただいておりますが、この書籍の価値をmacOS 10.15上でも維持するためにこの簡易PDFリーダーを作成する必要に迫られました。こんなバグをAppleが作らなければ必要のなかった作業です。

レポートずみではありますが、Appleがこのバグを認識して早期に修正することを希望してやみません。

Posted in PDF URL | Tagged 10.15savvy | 4 Comments

Tanzakuの実証実験用バージョン「Tanshio」の配布を開始

Posted on 11月 6, 2019 by Takaaki Naganoya

Tanzakuの実証試験用バージョン「Tanshio」の配布を開始しました。Tanzakuは、Piyomaru Softwareが開発中の自然言語インタフェース系自動処理プログラムの最新シリーズです。

# Tanzakuの派生シリーズや実験プログラムは「Tan-XXX」と命名します。

TanshioはTanzakuで予定している各種機能の縮小版をひととおり実装して、使い勝手を調べたりユーザー環境でうまく動作するかといったことを検証するためのものです。本バージョンには2020/1/31までの動作期限を設けています。

–> Download Tanshio(70KB)

Tanzakuの動作原理は、「ファイル名にユーザーが行いたい内容を記述することで、Tanzakuプログラムがそれを解釈して実行する」というものです(Talking Droplet)。

Tanshioでは、指定アプリケーションのメニュー項目をファイル名に記述することで、その項目をクリックします。

アプリケーション名>メニュー名>メニュー項目名1>メニュー項目名2

のように、階層メニュー名称をファイル名に記述することで、一切のプログラミングなしにメニュー項目の自動クリックを実現するものです。プログラミングを一切行わず、ファイル名を付け替えるだけでメニューのクリックを行い、ファイル名の指定が正しくない場合にはアプリケーション名、メニュー名などをユーザーに問い合わせることで、正しいファイル名を自動フィードバックします。

コマンドをファイル名から取得し、ファイル名のつけかえにより動作を変更し(可塑性のあるプログラム)、いったんコマンドを受理したあとはツールのように振る舞うといったTanzakuの主な特徴を備えています。さらに、入力コマンド(ファイル名)を誤った場合にはユーザーに対して選択肢を提示して正しいコマンドへと誘導する仕様もこのTanshioに実装。実際に使ってみてウザくないかといった確認を行うためのテストベッドです。

動作OSバージョンはmacOS 10.14.6および10.15.xですが、10.14を推奨します。また、システム環境設定の「セキュリティとプライバシー」で、「アクセシビリティ」「オートメーション」などの項目にTanshioを登録する必要があるため、実行・運用にあたってはシステム管理者権限が必要になります。


▲初期状態のTanshio


▲初期状態だとファイル名にアプリケーション名やメニュー項目名が書かれていないため、エラーになる


▲操作アプリケーション選択


▲操作メニュー選択


▲操作メニュー項目選択(コマンドにたどり着くまで繰り返し)


▲Tanshioが自分自身のファイル名を書き換え、正しく、アプリケーション名やメニュー名、メニュー項目名などが記入される。

この状態でTanshioを実行すると、目的のアプリケーションのメニューを操作します。


▲別のアプリケーションや別のメニュー項目をクリックするように変更したい場合にはファイル名を「Tanshio」のような短い名前に付け替えると起動時にアプリケーション選択/メニュー選択を行う

実際に使ってみて

同じメニュー項目をトグルで差し替えるような処理を行っているアプリケーションだと(例:CotEditorの「表示」>「行番号を表示」/「行番号を非表示」)、メニュー項目名をそのまま指定しても、実行2回目になるとメニュー項目が存在していないので、エラーになってアプリケーション選択とメニュー選択のやり直しになるようです。

なーるーほーどー(汗)

Posted in GUI Scripting | Tagged 10.14savvy 10.15savvy | 2 Comments

指定フォルダ以下のすべてのファイルとフォルダ名から絵文字を除去する v2

Posted on 11月 5, 2019 by Takaaki Naganoya

指定フォルダ以下のすべてのファイル名とフォルダ名から絵文字を除去するAppleScriptです。Shane StanleyのremoveEmojiルーチンを使っています。

macOS 10.14.1で絵文字が大幅に追加されたため、これらの絵文字をファイル名に用いていた場合には10.14.1以下のバージョンのOS環境にそのままファイルを持っていくことができません。

 Zipアーカイブ → 展開時にエラー
 DiskImageにコピーするファイルを格納し、古いOSに持って行ってドライブとしてマウントしてファイルコピー → コピーできない(エラー)

という状態になります。絵文字自体に害はないのですが、規格がコロコロ変わる(追加される)ことで、ファイル名に用いるのには問題があるということでしょう。


▲もともとのファイル名、フォルダ名。絵文字を大量に使用している(普段はファイル名に絵文字は使っていません)


▲本Scriptで一括で処理したファイル名、フォルダ名。害のない1️⃣2️⃣3️⃣などの文字だけは残る

実際に作ってみたら、aliasに対するリネームはしょっちゅう行ってきたものの、POSIX pathを用いて指定フォルダ以下すべてをリネームするようなScriptは組んでいなかったので、ちょっと考えさせられました。


▲本Scriptでリネームして、CotEditorのScript PackをmacOS 10.13.6の環境に持っていけました。ただ、絵文字がないと寂しい感じがします

指定フォルダ以下のファイル/フォルダを一括取得するのに、今回はあえてSpotlightを使っていません。ファイルサーバー上のファイル/フォルダを処理する可能性がありそうなのと、外部ライブラリを使わないほうがよいと考え、このような構成になっています。

AppleScript名:指定フォルダ以下のすべてのファイルとフォルダ名から絵文字を除去する v2.scptd
—
—  Created by: Takaaki Naganoya
—  Created on: 2019/11/04
—
—  Copyright © 2019 Piyomaru Software, All Rights Reserved
—

use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

property |NSURL| : a reference to current application’s |NSURL|
property NSString : a reference to current application’s NSString
property NSPredicate : a reference to current application’s NSPredicate
property NSFileManager : a reference to current application’s NSFileManager
property NSMutableArray : a reference to current application’s NSMutableArray
property NSRegularExpressionSearch : a reference to current application’s NSRegularExpressionSearch
property NSURLBookmarkResolutionWithoutUI : a reference to current application’s NSURLBookmarkResolutionWithoutUI

set aFol to POSIX path of (choose folder)

set anArray to NSMutableArray’s array()
set erArray to NSMutableArray’s array()
set aPath to NSString’s stringWithString:aFol
set dirEnum to NSFileManager’s defaultManager()’s enumeratorAtPath:aPath

repeat
  set aName to (dirEnum’s nextObject())
  
if aName = missing value then exit repeat
  
set aFullPath to aPath’s stringByAppendingPathComponent:aName
  
  
anArray’s addObject:aFullPath
end repeat

—逆順に(フォルダの深い場所&ファイル名から先に処理)
set revArray to (anArray’s reverseObjectEnumerator()’s allObjects()) as list

—リネーム
repeat with i in revArray
  set j to (NSString’s stringWithString:(contents of i))
  
set curName to j’s lastPathComponent() as string
  
set newName to removeEmoji(curName) of me
  
  
if curName is not equal to newName then
    set fRes to renameFileItem(j as string, newName) of me
    
if fRes = false then
      (erArray’s addObject:{j, newName})
    end if
  end if
end repeat

return erArray as list —リネームできなかったパス(フルパス、リネームするはずだった名称)

—絵文字除去
on removeEmoji(aStr)
  set aNSString to NSString’s stringWithString:aStr
  
return (aNSString’s stringByReplacingOccurrencesOfString:"[\\U0001F600-\\U0001F64F\\U0001F300-\\U0001F5FF\\U0001F680-\\U0001F6FF\\U00002600-\\U000026FF\\U00002700-\\U000027BF\\U0000FE00-\\U0000fE0F\\U0001F900-\\U0001F9FF\\U0001F1E6-\\U0001F1FF\\U00002B50-\\U00002B50\\U0000231A-\\U0000231B\\U00002328-\\U000023FA\\U000024C2-\\U000024C2\\U0001F194-\\U0001F194\\U0001F170-\\U0001F251\\U000025AB-\\U000025FE\\U00003297-\\U00003299\\U00002B55-\\U00002B55\\U00002139-\\U00002139\\U00002B1B-\\U00002B1C\\U000025AA-\\U000025AA\\U0001F004-\\U0001F004\\U0001F0CF-\\U0001F0CF]" withString:"" options:(NSRegularExpressionSearch) range:{0, aNSString’s |length|()}) as text
end removeEmoji

—ファイル/フォルダのリネーム
on renameFileItem(aPOSIXPath, newName)
  set theNSFileManager to NSFileManager’s defaultManager()
  
set POSIXPathNSString to NSString’s stringWithString:(aPOSIXPath)
  
  
–Make New File Path
  
set anExtension to POSIXPathNSString’s pathExtension()
  
set newPath to (POSIXPathNSString’s stringByDeletingLastPathComponent()’s stringByAppendingPathComponent:newName) –’s stringByAppendingPathExtension:anExtension
  
  
–Rename
  
if theNSFileManager’s fileExistsAtPath:newPath then
    return true
  else
    set theResult to theNSFileManager’s moveItemAtPath:POSIXPathNSString toPath:newPath |error|:(missing value)
    
if (theResult as integer = 1) then
      return (newPath as string)
    else
      return false
    end if
  end if
end renameFileItem

★Click Here to Open This Script 

Posted in file File path regexp Text | Tagged 10.14savvy 10.15savvy NSFileManager NSMutableArray NSPredicate NSRegularExpressionSearch NSString NSURL | Leave a comment

文字列から絵文字を削除

Posted on 11月 4, 2019 by Takaaki Naganoya

Shane Stanleyから投稿してもらった(いただいた)実用レベルの絵文字削除ルーチンです。

いろんな方面からツッコミが入って、徐々に実用レベルに到達するのではないかと予想していた絵文字削除ルーチン、いきなり最終兵器的なルーチンの登場により、「これでいいでしょ?」と思えるレベルに到達しました。

AppleScript名:remove emoji v0
—
—  Created by: Shane Stanley
—  Created on: 2019/11/04
—
use AppleScript version "2.7" — High Sierra (10.13) or later
use framework "Foundation"
use scripting additions

removeEmoji("2203)🔄❎⚙️🇯🇵簡易日本語形態素解析📚してそれっぽく❌伏せ字に(□に置き換え).scptd") of me
–> "2203)簡易日本語形態素解析してそれっぽく伏せ字に(□に置き換え).scptd"

on removeEmoji(aStr)
  set aNSString to current application’s NSString’s stringWithString:aStr
  
— Emoticons, Misc Symbols and Pictographs, Transport and Map, Misc symbols, Dingbats, Variation Selectors, Supplemental Symbols and Pictographs, Flags
  
return (aNSString’s stringByReplacingOccurrencesOfString:"[\\U0001F600-\\U0001F64F\\U0001F300-\\U0001F5FF\\U0001F680-\\U0001F6FF\\U00002600-\\U000026FF\\U00002700-\\U000027BF\\U0000FE00-\\U0000fE0F\\U0001F900-\\U0001F9FF\\U0001F1E6-\\U0001F1FF]" withString:"" options:(current application’s NSRegularExpressionSearch) range:{0, aNSString’s |length|()}) as text
end removeEmoji

★Click Here to Open This Script 

ただ、そこは一応「すべての絵文字」を入力して試しておく必要を感じるところ。手作業でぽちぽちキーボードから(絵文字バレットから)絵文字を入力しまくって本ルーチンで処理してみたところ、どうも文字コード上の「飛び地」にあるとおぼしき絵文字が消えません。

なので、削除テーブル部分に「消えなかった絵文字のコード」をこれでもかと追加しまくり、削除対象文字テーブルを強化してみました。一部、対象文字よりもひろめに削除範囲が指定されている箇所もありますが、本ルーチンは主にファイル名に対して使用された絵文字を除去してファイルの後方互換性を確保すること(最新のOSよりも古いバージョンのOSとファイルを安全に交換すること)が目的なので、そんなマイナー記号類が削除されても気にしないことにします。

すべての絵文字が削除されたわけではないといいますか、絵文字っぽい文字でなくなっただけで残っていたりもするのですが、今度はこれらの文字を消すと実害もありそうなので、現状ではこのぐらいでよいかと思われます。

もちろん、すぐにCotEditorのメニューに突っ込んで、選択範囲の絵文字を削除できるようにしておきました。

AppleScript名:remove emoji.scptd
—
—  Created by: Shane Stanley
—  Created on: 2019/11/04
—  Modified by: Takaaki Naganoya (Emoji Table Data)

use AppleScript version "2.7" — High Sierra (10.13) or later
use framework "Foundation"
use scripting additions

set allEmoji to "☺️☹️☠️✌️☝️🖐✍️1️⃣2️⃣3️⃣⚙️😀😃😄😁😆😅😂🤣☺️😊😇🙂🙃😉😌😍🥰😘😗😙😚😋😛😝😜🤪🤨🧐🤓😎🤩🥳😏😒😞😔😟😕🙁☹️😣😖😫😩🥺😢😭😤😡🤬🤯😳🥵🥶😱😨😨😰😥😓🤗🤔🤭🤫🤥😶😐😑😬🙄😯😦😧😮😲😴🤤😪😵🤐🥴🤢🤮🤧😷🤒🤕🤑🤠😈👿👹👺🤡💩👻💀☠️👽👾🤖🎃😺😸😹😻😼😽🙀😿😾🤲👐🙌👏🤝👍👎👊✊🤛🤜🤞✌️🤟🤘👌👈👉👆👇☝️✋🤚🖐🖖👋🤙💪🖕✍️🙏🦶🦵🐶🐱🐭🐰🦊🐻🐼🐨🐯🦁🐮🐷🐽🐸🐵🙈🙉🙊🐒🐔🐧🐦🐤🐣🐥🦆🦅🦉🦇🐗🐴🦄🐝🐛🦋🐌🐞🐜🦟🦗🕷🕸🦂🐍🦎🦖🐙🦑🦐🦞🦀🐡🐟🐬🐳🦈🐊🐅🐆🦓🦍🐘🦛🦏🐪🐫🦘🐃🐂🐄🐎🐏🐑🦙🐐🦌🐕🐩🐈🐓🦃🦚🦜🦢🕊🐇🦝🦡🐁🐀🐿🐲🌵🎄🌲🌳🌴🌱🌿🍀🍃🍂🍁🍄🌾🌹🥀🌺🌼🌻🌚🌕🌖🌗🌘🌒🌎💫⭐️🌟⚡️☄️💥🔥🌪🌈🌤⛅️🌥☁️🌦🌧⛈🌩❄️☃️🌬💨💧💦☔️🌫🍏🍎🍐🍊🍋🍌🍇🍓🍈🍒🍑🍍🥥🥝🍅🍆🥑🌶🌽🥕🥔🍠🥐🥯🍞🥖🥨🧀🥚🍳🥞🥓🥩🍗🍖🦴🦴🌭🍕🥪🥙🌮🌯🥗🥘🥫🍝🍜🍛🍣🍱🍤🍙🍚🍘🍥🥠🥮🍢🍦🍰🎂🍭🍬🍫🍿🍩🍪🌰🥜🍯🥛🍼☕️🍵🥤🍶🍺🍻🥂🍷🥃🍸🍹🍾🥄🍴🍽🥣🥡🥢🧂🏀⚾️🥎🎾🏐🏉🥏🎱🏓🏸🏒🏏⛳️🏹🎣🥊🥋🎽🛹🛷⛸🥌🎿⛷🏂🏋️‍♂️🤼‍♀️🤼‍♂️🤺🤾‍♀️🤾‍♂️🏌️‍♀️🏌️‍♂️🏇🧘‍♀️🧘‍♂️🏄‍♀️🏄‍♂️🏊‍♀️🏊‍♂️🤽‍♀️🤽‍♂️🚣‍♀️🚣‍♂️🧗‍♀️🧗‍♂️🚵‍♀️🚵‍♂️🚴‍♀️🚴‍♂️🏆🥇🥈🥉🏅🎖🏵🎗🎫🎟🎪🤹‍♀️🤹‍♂️🎭🎨🎬🎤🎧🎼🎹🥁🎷🎺🎸🎻🎲♟🎯🎳🎮🎰🧩🚗🚙🚌🚎🏎🚓🚑🚒🚐🚛🚜🛴🚲🏍🚨🚔🚍🚘🚖🚡🚠🚟🚃🚋🚞🚝🚄🚅🚈🚂🚆🚇🚊✈️🛫🛬🛩💺🛰🚀🛸🚁🛶⛵️🚤🛳⛴🚢⚓️⛽️🚧🚦🚥🚏🗺🗿🗽🗼🏰🏯🏟🎡🎢🎠⛲️⛱🏖🏝🏜🌋⛰🏔🗻🏕🏠🏡🏘🏚🏗🏭🏢🏬🏣🏤🏥🏦🏨🏪🏫🏩💒🏛⛪️🕌🕍🕋⛩🛤🗾🎑🏞🌅🌄🌠🎇🎆🌇🌆🏙🌃🌌🌉🌁⌚️📱📲💻⌨️🖥🖨🖱🖲🕹🗜💽💾💿📀📼📷📸📹🎥📽🎞📞☎️📟📠📺📻🎙🎚🎛🧭⏱⏲⏰🕰⌛️⏳📡🔋🔌💡🔦🕯🧯🛢💸💵💴💰💳💎⚖️🧰🔧🔨⚒🛠⛏🔩🧱⛓🧲💣🧨🔪🗡⚔️🛡🚬⚰️⚱️🏺🔮📿🧿💈🔭🔬🕳💊💉🧬🦠🧫🧪🌡🧹🧺🧻🚽🚰🚿🛁🛀🧼🧽🧴🛎🔑🗝🚪🛋🛌🧸🖼🛒🎁🎈🎏🎀🎊🎉🎎🏮🎐✉️📩📨📧💌📥📤📦🏷📪📫📬📭📮📯📜📃📄📑🧾📊📈📉🗒🗓📆📅🗑📇🗳🗄📋📁📂🗂🗞📰📓📔📒📕📗📘📙📚📖🔖🧷🔗📎🖇📐📏🧮📌📍✂️🖊🖋✒️🖌🖍📝✏️🔍🔎🔏🔐🔒🔓🔓❤️🧡💛💚💙💜🖤💔❣️💕💞💗💖💘💝💟☮️✝️☪️🕉☸️✡️🔯🕎☯️☦️🛐⛎♈️♉️♊️♋️♌️♍️♎️♏️♐️♑️♒️♓️🆔⚛️🉑☣️📴📳🈶🈚️🈸🈺🈷️✴️🆚💮🉐㊙️㊗️🈴🈵🈲🅰️🅱️🆎🆑🅾️🆘❌⭕️🛑⛔️📛🚫💯💢♨️🚷🚯🚱🔞📵🚭❗️❕❓❓❔‼️⁉️🔅🔆〽️⚠️🚸🔱⚜️🔰✅🈯️💹❇️✳️❎🌐💠Ⓜ️🌀💤🏧♿️🅿️🈳🈂️🛂🛃🛄🛅🚹🚺🚼🚻🚮🎦📶🈁🔣ℹ️🔤🔡🔠🆖🆗🆙🆙🆒🆕🆓0️⃣1️⃣2️⃣3️⃣4️⃣5️⃣6️⃣7️⃣8️⃣9️⃣🔟🔢#️⃣*️⃣▶️⏸⏯⏹⏺⏭⏮⏩⏪⏫⏬🔼🔽⬅️⬇️↘️↖️↕️↙️↗️⬆️➡️◀️⏏️↪️↩️↔️⤵️🔀🔁🔄🔃⤵️⤴️🎵🎶➕➖➗♾💲💱©️👁‍🗨🔚🔙🔛🔝🔜➰➿➿☑️🔘🔴🔵🔺🔻🔸🔹🔶🔷🔳🔲▫️◽️◻️⬜️🔈🔇🔉🔊🔔🔔🔔🔕📣📢💬💭🗯♣️♦️🃏🎴🕐🕑🕒🕓🕔🕕🕖🕗🕘🕙🕚🕛🕜🕝🕞🕟🕠🕡🕢🕣🕤🕥🕥🕦🕧🀄️♥️♠️⬛️◼️◾️▪️⚪️✔️〰️®️™️✖️⭐️🏴🏁🏴‍☠️🏳️🚩🏳️‍🌈🇺🇳🇮🇸🇮🇪🇦🇿🇦🇫🇺🇸🇦🇪🇩🇿🇦🇷🇦🇼🇦🇱🇦🇲🇦🇮🇦🇴🇦🇬🇦🇩🇾🇪🇬🇧🏴󠁧󠁢󠁳󠁣󠁴󠁿🏴󠁧󠁢󠁷󠁬󠁳󠁿🇮🇱🇮🇹🇮🇶🇮🇷🇮🇳🇮🇩🇼🇫🇺🇬🇺🇦🇺🇿🇺🇾🇪🇨🇪🇬🇪🇪🇪🇹🇪🇷🇸🇻🇦🇺🇦🇹🇦🇽🇴🇲🇳🇱🇧🇶🇬🇭🇨🇻🇬🇬🇬🇾🇰🇿🇶🇦🇨🇦🇮🇨🇬🇦🇨🇲🇬🇲🇰🇭🇬🇳🇬🇼🇨🇾🇨🇺🇨🇺🇨🇼🇬🇷🇰🇮🇰🇬🇬🇹🇬🇵🇬🇺🇰🇼🇨🇰🇬🇱🇨🇽🇬🇩🇭🇷🇰🇾🇰🇪🇨🇮🇨🇨🇨🇨🇨🇷🇽🇰🇰🇲🇨🇴🇨🇬🇨🇩🇸🇦🇬🇸🇼🇸🇧🇱🇸🇹🇿🇲🇵🇲🇸🇲🇸🇱🇩🇯🇬🇮🇯🇪🇯🇲🇬🇪🇸🇾🇸🇬🇸🇽🇿🇼🇨🇭🇸🇪🇸🇩🇪🇸🇸🇷🇱🇰🇸🇰🇸🇮🇸🇿🇸🇨🇸🇳🇷🇸🇰🇳🇻🇨🇸🇭🇸🇴🇸🇧🇹🇨🇹🇭🇹🇯🇹🇿🇨🇿🇹🇩🇹🇳🇨🇱🇹🇻🇩🇰🇩🇪🇹🇬🇹🇰🇩🇴🇩🇲🇹🇹🇹🇲🇹🇷🇹🇴🇳🇬🇳🇷🇳🇦🇳🇺🇳🇮🇳🇮🇳🇪🇳🇨🇳🇿🇳🇵🇳🇫🇳🇫🇳🇴🇧🇭🇭🇹🇵🇰🇻🇦🇵🇦🇻🇺🇧🇸🇵🇬🇧🇲🇵🇼🇵🇾🇧🇧🇭🇺🇧🇩🇵🇳🇫🇯🇵🇭🇫🇮🇧🇹🇵🇷🇫🇴🇫🇰🇧🇷🇫🇷🇧🇬🇧🇫🇧🇳🇧🇮🇻🇳🇧🇯🇻🇪🇧🇾🇧🇿🇵🇪🇧🇪🇵🇱🇧🇦🇧🇼🇧🇴🇵🇹🇭🇳🇲🇭🇲🇴🇲🇰🇲🇬🇾🇹🇲🇼🇲🇱🇲🇹🇲🇶🇲🇾🇮🇲🇫🇲🇲🇲🇲🇽🇲🇺🇲🇷🇲🇿🇲🇨🇲🇻🇲🇩🇲🇦🇲🇳🇲🇪🇲🇸🇯🇴🇱🇦🇱🇻🇱🇹🇱🇾🇱🇮🇱🇷🇷🇴🇱🇺🇷🇼🇱🇸🇱🇧🇷🇪🇷🇺🇮🇴🇻🇬🇰🇷🇪🇺🇰🇷🇭🇰🇪🇭🇬🇶🇨🇫🇨🇳🇹🇱🇿🇦🇸🇸🇦🇶🇯🇵🎌🇬🇫🇵🇫🇹🇫🇻🇮🇦🇸🇲🇵🇰🇵"
removeEmoji(allEmoji) of me
–> "1⃣2⃣3⃣‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‼⁉〽0⃣1⃣2⃣3⃣4⃣5⃣6⃣7⃣8⃣9⃣#⃣*⃣⬅⬇↘↖↕↙↗⬆↪↩↔⤵⤵⤴©‍〰®™‍‍󠁧󠁢󠁳󠁣󠁴󠁿󠁧󠁢󠁷󠁬󠁳󠁿"

on removeEmoji(aStr)
  set aNSString to current application’s NSString’s stringWithString:aStr
  
  
— Emoticons, Misc Symbols and Pictographs, Transport and Map, Misc symbols, Dingbats, Variation Selectors, Supplemental Symbols and Pictographs, Flags
  
return (aNSString’s stringByReplacingOccurrencesOfString:"[\\U0001F600-\\U0001F64F\\U0001F300-\\U0001F5FF\\U0001F680-\\U0001F6FF\\U00002600-\\U000026FF\\U00002700-\\U000027BF\\U0000FE00-\\U0000fE0F\\U0001F900-\\U0001F9FF\\U0001F1E6-\\U0001F1FF\\U00002B50-\\U00002B50\\U0000231A-\\U0000231B\\U00002328-\\U000023FA\\U000024C2-\\U000024C2\\U0001F194-\\U0001F194\\U0001F170-\\U0001F251\\U000025AB-\\U000025FE\\U00003297-\\U00003299\\U00002B55-\\U00002B55\\U00002139-\\U00002139\\U00002B1B-\\U00002B1C\\U000025AA-\\U000025AA\\U0001F004-\\U0001F004\\U0001F0CF-\\U0001F0CF]" withString:"" options:(current application’s NSRegularExpressionSearch) range:{0, aNSString’s |length|()}) as text
end removeEmoji

★Click Here to Open This Script 

Posted in regexp Text | Tagged 10.14savvy 10.15savvy NSRegularExpressionSearch NSString | 2 Comments

文字列から絵文字のみ削除するじっけん

Posted on 11月 3, 2019 by Takaaki Naganoya

文字列から絵文字だけを削除する実験です(まだ、完全にはうまく削除しきれていないので)。

→ きちんと動くバージョン 文字列から絵文字を削除

CotEditorのScript PackをmacOS 10.13環境にインストールしようとして、Zipアーカイブが展開できないとか、ファイル名に絵文字が入っているためにファイルコピー自体が行えないという現象に直面しました。

macOS 10.14.1で絵文字が70個ほど追加になったという記録があるので、

macOS Mojave 10.14.1
アップデートこのアップデートの内容は以下の通りです。グループ FaceTime のビデオ通話とオーディオ通話に対応しました。最大 32 人の参加者が同時に通話することができ、会話の内容はエンドツーエンドで暗号化されるため、プライバシーも守られます。グループ FaceTime は、メッセージ App のグループチャットから始めることができ、通話に途中から参加することもできます。赤毛、白髪、巻き毛の新しいキャラクター、スキンヘッドの新しい絵文字、より感情豊かな笑顔のほか、動物、スポーツ、食べ物を表す絵文字など、70 種類以上の絵文字が新たに追加されました。

これらの絵文字ファイル名に用いたファイルをmacOS 10.14.1以降のOS上で作成し、それ以前のバージョンのOSに持って行こうとするとトラブルが発生するのは当然のことでしょう。

ファイル名が原因であることは明らかなので(ふだんはScriptのファイル名に絵文字を使うことはありません)、自動で指定フォルダ以下の書類やフォルダをすべてスキャンして絵文字キャラクターを削除するという処理を書こうとして、その前段階として指定文字列中の絵文字を削除するAppleScriptを書いてみました。

いい感じに削除できていると思えば、削除できない文字もあったりで、まだ完全ではない印象です。p{Emoji_Presentation}で検出しているのですが、検出できない絵文字があるようで、、、、

もともと、NSString上で完結するように処理しようと試みていたものの、matchesInStringで絵文字を検出したあとの消し込みにてこずって、「じゃあ、1文字ずつ分解してチェックルーチンを通せばいいや」と、トーンダウンした経緯があります。

特定の絵文字だけ検出できないというのは納得できません。本Scriptはまだ実戦配備できないレベルだと思います。

下手をすると、全絵文字の全パターンを配列(リスト)に入れておいて、存在確認を行うことになるのかも?

AppleScript名:テキストから絵文字抽出(複数)して削除.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2019/11/03
—
–  Copyright © 2019 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set a1Str to "where’s 😎the 😎😎emoji 😎?"
set b1Str to removeEmoji(a1Str) of me
–> "where’s the emoji ?"

set a2Str to "2200)🔄❎すべて❌伏せ字に(□に置き換え)"
set b2Str to removeEmoji(a2Str) of me
–> "2200)すべて伏せ字に(□に置き換え)"

set a3Str to "2251)1️⃣2️⃣3️⃣昇順(1–>9)で連番を振る"
set b3Str to removeEmoji(a3Str) of me
–> "2251)1️⃣2️⃣3️⃣昇順(1–>9)で連番を振る"

set a4Str to "2203)🔄❎⚙️🇯🇵簡易日本語形態素解析📚してそれっぽく❌伏せ字に(□に置き換え).scptd"
set b4Str to removeEmoji(a4Str) of me
–> "2203)⚙️簡易日本語形態素解析してそれっぽく伏せ字に(□に置き換え).scptd"

set emojiStr to "😀😃😄😁😆😅😂🤣☺️😊😇🙂🙃😉😌😍🥰😘😗😙😚😋😛😝😜🤪🤨🧐🤓😎🤩🥳😏😒😞😔😟😕🙁☹️😣😖😫😩🥺😢😭😤😡🤬🤯😳🥵🥶😱😨😨😰😥😓🤗🤔🤭🤫🤥😶😐😑😬🙄😯😦😧😮😲😴🤤😪😵🤐🥴🤢🤮🤧😷🤒🤕🤑🤠😈👿👹👺🤡💩👻💀☠️👽👾🤖🎃😺😸😹😻😼😽🙀😿😾🤲👐🙌👏🤝👍👎👊✊🤛🤜🤞✌️🤟🤘👌👈👉👆👇☝️✋🤚🖐🖖👋🤙💪🖕✍️🙏🦶🦵"
set eRes to removeEmoji(emojiStr) of me
–> "☺️☹️☠️✌️☝️🖐✍️"

set emojiStr to "☺️☹️☠️✌️☝️🖐✍️1️⃣2️⃣3️⃣⚙️"
set eRes to removeEmoji(emojiStr) of me
–> "☺️☹️☠️✌️☝️🖐✍️1️⃣2️⃣3️⃣⚙️"

on removeEmoji(aStr)
  script spdC
    property outList : {}
    
property cList : {}
  end script
  
  
set (cList of spdC) to characters of aStr
  
log (cList of spdC)
  
set (outList of spdC) to {}
  
  
repeat with i in (cList of spdC)
    set j to contents of i
    
set eRes to chkEmoji(j) of me
    
if eRes = false then
      set the end of (outList of spdC) to j
    end if
  end repeat
  
return retDelimedText((outList of spdC), "") of me
end removeEmoji

on chkEmoji(aStr)
  set aNSString to current application’s NSString’s stringWithString:aStr
  
set aReg to current application’s NSRegularExpression’s regularExpressionWithPattern:"\\p{Emoji_Presentation}" options:0 |error|:(missing value)
  
set cResList to (aReg’s matchesInString:(aNSString) options:0 range:(current application’s NSMakeRange(0, aNSString’s |length|()))) as list
  
if length of cResList = 0 then return false –指定文字列に絵文字が入っていない場合
  
return true –指定文字列に絵文字が入っている場合
end chkEmoji

on retDelimedText(aList, aDelim)
  set aText to ""
  
set curDelim to AppleScript’s text item delimiters
  
set AppleScript’s text item delimiters to aDelim
  
set aText to aList as text
  
set AppleScript’s text item delimiters to curDelim
  
return aText
end retDelimedText

★Click Here to Open This Script 

Posted in Text | Tagged 10.14savvy 10.15savvy NSMakeRange NSRegularExpression NSString | Leave a comment

個人的に腹がたったmacOS/AppleScriptのバグ・ワースト10

Posted on 11月 2, 2019 by Takaaki Naganoya

歴代のmacOS自体やAppleScriptの処理系で腹が立ったバグについてまとめてみましょう。腹の立ったOSのバージョンでいえばmacOS 10.13が歴代でダントツの1位ですが、10.15もリリースされる前から「これはひどい」「10.13と同じぐらいひどい」とわかっていたので、10.14.6を選択。

10個にしぼりこむことがたいへんに難しかったですし、未修正の問題などもあり非常に腹立たしいかぎりです。たぶん、ワースト3以下は僅差だと思います。

Mac OS X 10.3でディスク暗号化機能の「FileVault」が登場したときには、野生のカンで「これは使ってはいけない機能だ」と様子見していたところ、見事に「FileVaultをオンにしたホームディレクトリ下のファイルにAppleScriptからアクセスできないバグがある」ことが発覚。以来、これをオンにしたことがありません(最近でも、macOS 10.15でFileVaultをオンにしているとMac mini 2018に接続したeGPUから出力できないとか。なかなか信用できません)。

1位:Mac OS X 10.3 “is in”演算子が動かないバグ(AppleScript)【修正済み】

こんな基礎的な演算子でバグ作られた日には何もできません。とくに、is in演算子はデータの「ゆらぎ吸収」処理に使いまくっているので、WWDCにハリセン持って担当者をブン殴りに行こうかと真剣に悩みました。

2位:macOS 10.13〜 PDFViewのcurrentPageを取得するとおかしな値が返ってくるバグ(Bug Reportしても直らない。殺意を覚える)(Scripting Bridge)【未修正】

これのせいで、自分のMac App Storeにアップしたアプリケーションが動かなくなりましたわ。ひどい。ひどすぎる。しかも、ちゃんとDevelopper Supportに連絡しても返事ひとつなく、修正もされません。Developer Support、仕事してませんよね。

# Developper Support、ほんっとになんにもしない。あの部署、何のためにあるんだろう? 今度、インシデントを消費して「じゃあ逆に、君たちは何をしてくれやがる部署ですか? 後学のために教えてくださいませ」と聞いてみようか

3位:OS X 10.9 ntpのシンクロが狂うバグ(OS)【修正済み】

当時、Mac App Storeに出す時計アプリを作ってたんですが、これのせいで時計が合わずに冷や汗かかされていました。まさかOSのバグとは、、、、おかげでアプリはお蔵入りしてしまいました。

4位:macOS 10.12〜 Input Methodのバグか何かで、ファイル名に不可視文字が混入するケースがあり、混入するとファイル名の文字列に追記するとエラーに(OS、日本語環境)【未修正】

これは、レポートもしていますし、波及範囲が広いので大問題なのですが、直っていませんね。認識すらしていないんじゃないでしょうか。

5位:Mac OS X 10.0〜10.4 File Pathの文字エンコーディングが変で日本語ファイルパスの扱いに慎重になる必要があったバグ(OS、AppleScript)【修正済み】

ファイルパスをas stringしてからas unicode textでcastさせられたりと、煮湯を飲まされていました。10.5で根本的に修正されたものの、10.5で根本的な修正を行うまで、問題が放置されっぱなし(こういうの多いなー)。

6位:Mac OS X 10.0〜10.4 display dialogで日本語表示を行うさいに、国際化対応アプリケーションへのtellブロック内で実行しないと文字化けするバグ(OS、AppleScript)【修正済み】

さすがに困って担当者に問い合わせて回避方法は教えてもらいましたが、そういう付け焼き刃的な回避方法を知らせる前につぶしてほしいと感じるものでした。

7位:Mac OS X 10.4、Script MenuがSystem Eventsの管轄下にあったため、Script Menuから呼び出したScript内でSystem Eventsの機能を呼び出すと実行が止まるバグ(AppleScript)【修正済み】

Mail.appのメッセージ仕分けBot Scriptがこれにヒットしてひどい目に遭いました。毎日使っているものなので、とても大きなダメージを喰らいました。Mac OS X 10.5で修正されました。

8位:OS X 10.10、Cocoa Objectsの実数値をASにcastすると小数点以下が無視されるバグ(Scripting Bridge)【修正済み】

これもひどかったですね。Shaneが対処のためのFrameworkやライブラリを用意しなかったら、いまほどCocoaの機能が利用されていなかったと思います。このバグはmacOS 10.11で修正されました。

9位:macOS 10.12、AppleScript Dropletにドロップしたファイルで、ファイル拡張属性(Xattr)のcom.apple.quarantineがついている場合には処理されないバグ(放置、未修正)(OS、AppleScript)【未修正】

無責任さここに極まれりといったところです。Core OSとAppleScriptの処理系との間で仕様のすりあわせが行われておらず、チーム間でぜんぜん連携が取れていない(というよりも、チーム間で垣根を超えて折衝する権限がないっぽい)、現在のAppleの社内体勢そのものに問題があるんでしょう。これも、対処方法が一応は確立しているものの、たまりません。

10位:macOS 10.13、NSRectがレコードではなくリストで返ってくるように(告知なしで)変更される(Scripting Bridge)【未修正】

なんなんでしょうね、コレ。作業量が増えて腹が立つんですが、個人的にはSwiftのアップデートに合わせてAPI側の仕様を変えてしまったんじゃないんかとにらんでいます。

Posted in Bug | Leave a comment

Post navigation

  • Newer posts

電子書籍(PDF)をオンラインストアで販売中!

Google Search

Popular posts

  • macOS 13, Ventura(継続更新)
  • アラートダイアログ上にWebViewで3Dコンテンツを表示(WebGL+three.js)v3
  • Xcode 14.2でAppleScript App Templateを復活させる
  • UI Browserがgithub上でソース公開され、オープンソースに
  • macOS 13 TTS Voice環境に変更
  • 2022年に書いた価値あるAppleScript
  • ChatGPTで文章のベクトル化(Embedding)
  • 新発売:AppleScriptからSiriを呼び出そう!
  • iWork 12.2がリリースされた
  • 従来と異なるmacOS 13の性格?
  • 新発売:CotEditor Scripting Book with AppleScript
  • macOS 13対応アップデート:AppleScript実践的テクニック集(1)GUI Scripting
  • AS関連データの取り扱いを容易にする(はずの)privateDataTypeLib
  • macOS 13でNSNotFoundバグふたたび
  • macOS 12.5.1、11.6.8でFinderのselectionでスクリーンショット画像をopenできない問題
  • ChatGPTでchatに対する応答文を取得
  • 新発売:iWork Scripting Book with AppleScript
  • Finderの隠し命令openVirtualLocationが発見される
  • macOS 13.1アップデートでスクリプトエディタの挙動がようやくまともに
  • あのコン過去ログビューワー(暫定版)

Tags

10.11savvy (1101) 10.12savvy (1242) 10.13savvy (1390) 10.14savvy (586) 10.15savvy (434) 11.0savvy (277) 12.0savvy (186) 13.0savvy (59) CotEditor (60) Finder (47) iTunes (19) Keynote (99) NSAlert (60) NSArray (51) NSBezierPath (18) NSBitmapImageRep (20) NSBundle (20) NSButton (34) NSColor (51) NSDictionary (27) NSFileManager (23) NSFont (18) NSImage (41) NSJSONSerialization (21) NSMutableArray (62) NSMutableDictionary (21) NSPredicate (36) NSRunningApplication (56) NSScreen (30) NSScrollView (22) NSString (117) NSURL (97) NSURLRequest (23) NSUTF8StringEncoding (30) NSView (33) NSWorkspace (20) Numbers (57) Pages (38) Safari (41) Script Editor (20) WKUserContentController (21) WKUserScript (20) WKUserScriptInjectionTimeAtDocumentEnd (18) WKWebView (23) WKWebViewConfiguration (22)

カテゴリー

  • 2D Bin Packing
  • 3D
  • AirDrop
  • AirPlay
  • Animation
  • AppleScript Application on Xcode
  • beta
  • Bluetooth
  • Books
  • boolean
  • bounds
  • Bug
  • Calendar
  • call by reference
  • Clipboard
  • Code Sign
  • Color
  • Custom Class
  • dialog
  • drive
  • exif
  • file
  • File path
  • filter
  • folder
  • Font
  • Font
  • GAME
  • geolocation
  • GUI
  • GUI Scripting
  • Hex
  • History
  • How To
  • iCloud
  • Icon
  • Image
  • Input Method
  • Internet
  • iOS App
  • JavaScript
  • JSON
  • JXA
  • Keychain
  • Keychain
  • Language
  • Library
  • list
  • Locale
  • Machine Learning
  • Map
  • Markdown
  • Menu
  • Metadata
  • MIDI
  • MIME
  • Natural Language Processing
  • Network
  • news
  • Noification
  • Notarization
  • Number
  • Object control
  • OCR
  • OSA
  • PDF
  • Peripheral
  • PRODUCTS
  • QR Code
  • Raw AppleEvent Code
  • Record
  • rectangle
  • recursive call
  • regexp
  • Release
  • Remote Control
  • Require Control-Command-R to run
  • REST API
  • Review
  • RTF
  • Sandbox
  • Screen Saver
  • Script Libraries
  • sdef
  • search
  • Security
  • selection
  • shell script
  • Shortcuts Workflow
  • Sort
  • Sound
  • Spellchecker
  • Spotlight
  • SVG
  • System
  • Tag
  • Telephony
  • Text
  • Text to Speech
  • timezone
  • Tools
  • Update
  • URL
  • UTI
  • Web Contents Control
  • WiFi
  • XML
  • XML-RPC
  • イベント(Event)
  • 未分類

アーカイブ

  • 2023年9月
  • 2023年8月
  • 2023年7月
  • 2023年6月
  • 2023年5月
  • 2023年4月
  • 2023年3月
  • 2023年2月
  • 2023年1月
  • 2022年12月
  • 2022年11月
  • 2022年10月
  • 2022年9月
  • 2022年8月
  • 2022年7月
  • 2022年6月
  • 2022年5月
  • 2022年4月
  • 2022年3月
  • 2022年2月
  • 2022年1月
  • 2021年12月
  • 2021年11月
  • 2021年10月
  • 2021年9月
  • 2021年8月
  • 2021年7月
  • 2021年6月
  • 2021年5月
  • 2021年4月
  • 2021年3月
  • 2021年2月
  • 2021年1月
  • 2020年12月
  • 2020年11月
  • 2020年10月
  • 2020年9月
  • 2020年8月
  • 2020年7月
  • 2020年6月
  • 2020年5月
  • 2020年4月
  • 2020年3月
  • 2020年2月
  • 2020年1月
  • 2019年12月
  • 2019年11月
  • 2019年10月
  • 2019年9月
  • 2019年8月
  • 2019年7月
  • 2019年6月
  • 2019年5月
  • 2019年4月
  • 2019年3月
  • 2019年2月
  • 2019年1月
  • 2018年12月
  • 2018年11月
  • 2018年10月
  • 2018年9月
  • 2018年8月
  • 2018年7月
  • 2018年6月
  • 2018年5月
  • 2018年4月
  • 2018年3月
  • 2018年2月

https://piyomarusoft.booth.pm/items/301502

メタ情報

  • ログイン
  • 投稿フィード
  • コメントフィード
  • WordPress.org

Forum Posts

  • 人気のトピック
  • 返信がないトピック

メタ情報

  • ログイン
  • 投稿フィード
  • コメントフィード
  • WordPress.org
Proudly powered by WordPress
Theme: Flint by Star Verte LLC