AppleScript名:指定名称のアプリケーションプロセスが存在すればその正しい名前を返す |
— Created 2015-07-29 16:43:11 +0900 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set aName to "メール" set aRes to returnExactNameOfAnApp(aName) of me on returnExactNameOfAnApp(aName) tell application "System Events" set ap1List to every process whose name is equal to aName if ap1List = {} then set ap1List to every process whose displayed name is equal to aName if ap1List = {} then return false end if set anApp to contents of first item of ap1List return name of anApp end tell end returnExactNameOfAnApp |
カテゴリー: System
ASOCで現在実行中のプロセスの情報を取得
AppleScript名:ASOCで現在実行中のプロセスの情報を取得 |
— Created 2015-09-08 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set procInfo to current application’s NSProcessInfo’s processInfo() –> (NSProcessInfo) <NSProcessInfo: 0x6000000587e0> set argList to procInfo’s arguments() –> (NSArray) {"/Applications/ASObjC Explorer 4.app/Contents/MacOS/ASObjC Explorer 4"}–ASObjC Explorer 4 set envList to procInfo’s environment() –> (NSDictionary) {PATH:"/usr/bin:/bin:/usr/sbin:/sbin", TMPDIR:"/var/folders/h4/jfhlwst88xl9z0001s7k9vk00000gr/T/", LOGNAME:"me", HOME:"/Users/me", XPC_FLAGS:"0x0", Apple_PubSub_Socket_Render:"/private/tmp/com.apple.launchd.OLjjErklL4/Render", USER:"me", SSH_AUTH_SOCK:"/private/tmp/com.apple.launchd.y3g31V3gh7/Listeners", SECURITYSESSIONID:"XXXXX", DISPLAY:"/private/tmp/com.apple.launchd.63oTa9LGKM/org.macosforge.xquartz:0", XPC_SERVICE_NAME:"au.com.myriad-com.ASObjC-Explorer-4.69328", SHELL:"/bin/xxxx", __CF_USER_TEXT_ENCODING:"0x1F8:0x1:0xE"} set anUniqueStr to procInfo’s globallyUniqueString() –> (NSString) "XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX-XXXX-XXXXXXXXXXXXXXXX" set anID to procInfo’s processIdentifier() –> 4283–Unix Process ID (pid) set aName to procInfo’s processName() –> (NSString) "ASObjC Explorer 4" set aHostName to procInfo’s hostName() –> (NSString) "mbpretina.local" set aVersionStr to procInfo’s operatingSystemVersionString() –> (NSString) "バージョン 10.10.5(ビルド 14F27)" set aVersion to procInfo’s operatingSystemVersion() –> can’t bridge argument of type {_NSOperatingSystemVersion=qqq}. OS X 10.10ではブリッジ不可。10.11でOK set aCPUCores to procInfo’s processorCount() –> 8 set activeCPUCores to procInfo’s activeProcessorCount() –> 8 set anRAMcapacity to procInfo’s physicalMemory() –> 8.589934592E+9 set anRAMcapacity to procInfo’s systemUptime() –> 6.8344782485801E+4 set aThermalState to procInfo’s thermalState() –> 0 –NSProcessInfoThermalStateNominal (* enum { NSProcessInfoThermalStateNominal, NSProcessInfoThermalStateFair, NSProcessInfoThermalStateSerious, NSProcessInfoThermalStateCritical }; *) |
ASOCでプロセス情報を取得
AppleScript名:ASOCでプロセス情報を取得 |
— Created 2015-10-23 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" set runningApplications to (current application’s NSWorkspace’s sharedWorkspace()’s runningApplications()) as list repeat with i in runningApplications set aName to (i’s localizedName()) as text set anIcon to (i’s icon()) set anBundleID to (i’s bundleIdentifier()) as text set anBundleURL to (i’s bundleURL()) set tmpArch to (i’s executableArchitecture()) if tmpArch = 16777223 then set anArch to "X86_64" else if tmpArch = 7 then set anArch to "I386 " else set anArch to "Another Arch (PPC? or Error)" end if set anLaunchDate to (i’s launchDate()) set anFinishLaunch to (i’s isFinishedLaunching()) set aProcID to (i’s processIdentifier()) set anOwnMenubar to (i’s ownsMenuBar()) log {aName, tmpArch, anArch} –> (* {"CCLibrary", -1, "Another Arch (PPC?)"} *) end repeat |
NSRunningApplicationでアプリケーションプロセス情報を取得
AppleScriptで他のアプリケーションプロセスの情報を取得するには、OS標準装備のSystem Eventsに対して、
tell application "System Events" set aProp to properties of process "Safari" end tell --> {has scripting terminology:true, bundle identifier:"com.apple.Safari", file:alias "Macintosh HD:Applications:Safari.app:" of application "System Events", creator type:"sfri", subrole:missing value, entire contents:{}, selected:missing value, application file:alias "Cherry:Applications:Safari.app:" of application "System Events", orientation:missing value, role:"AXApplication", accepts high level events:true, file type:"APPL", value:missing value, position:missing value, id:909534, displayed name:"Safari", name:"Safari", class:application process, background only:false, frontmost:false, size:missing value, visible:true, Classic:false, role description:"application", maximum value:missing value, architecture:"x86_64", partition space used:0, short name:"Safari", focused:missing value, minimum value:missing value, help:missing value, title:"Safari", accepts remote events:false, total partition size:0, description:"application", accessibility description:missing value, enabled:missing value, unix id:3386}
などと操作することになります。ただし、System Eventsがつねに使えるわけではありません。
Mac App Storeに出すアプリケーションの中だと、些細な用途に他のアプリケーションを呼び出そうとしても、よほどの理由がないかぎり通りません(リジェクトされます)。Dark Mode/Light Modeの検出に安直にSystem Eventsを使おうとしてリジェクトされた経験があります。
そこで、他のサービス(shell commandとか、Cocoa Frameworkとか)を経由して機能を呼び出すことになります。まったく同じことができるわけではありませんが、1つの目的に対して複数の方法を用意しておくのはセオリーです。OS側でバグを作られた場合の回避策とか(正しくないOSバージョンを返してくるmacOSがありました)、目的に応じて利用に制限がかかる用途(まさにMac App Storeがそれです)があるためです。
AppleScript名:NSRunningApplicationでアプリケーションプロセス情報を取得 |
— Created 2017-09-17 15:27:23 +0900 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" set aBundleID to "com.apple.Safari" set appArray to current application’s NSRunningApplication’s runningApplicationsWithBundleIdentifier:aBundleID if appArray’s |count|() = 0 then its return set appItem to appArray’s objectAtIndex:0 set iconRes to (appItem’s icon()) (* –> (NSImage) <NSImage 0x618001478f40 Size={32, 32} Reps=( "<NSIconRefImageRep:0x61800029b300 iconRef=0x8403 size:128×128 pixels:128×128>", "<NSIconRefImageRep:0x61800089f7c0 iconRef=0x8403 size:128×128 pixels:256×256>", "<NSIconRefImageRep:0x6180006989c0 iconRef=0x8403 size:256×256 pixels:256×256>", "<NSIconRefImageRep:0x61800129f040 iconRef=0x8403 size:256×256 pixels:512×512>", "<NSIconRefImageRep:0x618000898dd0 iconRef=0x8403 size:512×512 pixels:512×512>", "<NSIconRefImageRep:0x61800029a270 iconRef=0x8403 size:48×48 pixels:48×48>", "<NSIconRefImageRep:0x618000691490 iconRef=0x8403 size:36×36 pixels:36×36>", "<NSIconRefImageRep:0x618000c8e100 iconRef=0x8403 size:36×36 pixels:72×72>", "<NSIconRefImageRep:0x618000a80460 iconRef=0x8403 size:32×32 pixels:32×32>", "<NSIconRefImageRep:0x6180004899c0 iconRef=0x8403 size:32×32 pixels:64×64>", "<NSIconRefImageRep:0x61800089db50 iconRef=0x8403 size:18×18 pixels:18×18>", "<NSIconRefImageRep:0x618001291530 iconRef=0x8403 size:18×18 pixels:36×36>", "<NSIconRefImageRep:0x618000c8e0b0 iconRef=0x8403 size:16×16 pixels:16×16>", "<NSIconRefImageRep:0x618000880280 iconRef=0x8403 size:16×16 pixels:32×32>", "<NSIconRefImageRep:0x618000e99a50 iconRef=0x8403 size:512×512 pixels:1024×1024>" )> *) set locRes to (appItem’s localizedName()) as string –> "Safari" set bID to (appItem’s bundleIdentifier()) as string –> "com.apple.Safari" set bURL to (appItem’s bundleURL()) as string –> "Cherry:Applications:Safari.app:" set arch to (appItem’s executableArchitecture()) –> 16777223 set exeURL to (appItem’s executableURL()) –> (NSURL) file:///Applications/Safari.app/Contents/MacOS/Safari set launchDate to (appItem’s launchDate()) –> (NSDate) 2017-09-13 01:58:16 +0000 set launchFinish to (appItem’s finishedLaunching) as integer –> 1 set pID to (appItem’s processIdentifier) as integer –> 11877 set oenMenu to (appItem’s ownsMenuBar()) as boolean –> false |
アプレットのアイコンをDockに出さない2
AppleScript名:アプレットのアイコンをDockに出さない2 |
— Created 2015-10-22 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" –Dockアイコン非表示、dialogも出ない current application’s NSApp’s setActivationPolicy:(current application’s NSApplicationActivationPolicyProhibited) repeat with i from 1 to 10 tell current application display notification (i as text) delay 1 end tell end repeat quit |
Dockとメニューバーを隠す→戻す
AppleScript名:Dockとメニューバーを隠す→戻す |
— Created 2017-03-15 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" –Main MenuとDockを隠す current application’s NSApplication’s sharedApplication()’s setPresentationOptions:10 –NSApplicationPresentationHideMenuBar | NSApplicationPresentationHideDock delay 10 –MenuとDockを通常に戻す current application’s NSApplication’s sharedApplication()’s setPresentationOptions:(current application’s NSApplicationPresentationDefault) |
Dockアイコンにプログレスバーを追加
Dockアイコンにプレグレスバーを描画するAppleScriptです。
書き方のクセがあからさまに違うこの内容は、Edama2さんからいただいたものですね、コレ。
AppleScript名:Dockアイコンにプログレスバーを追加 |
use AppleScript use framework "Foundation" use scripting additions on run set max to 100 repeat with num from 1 to max my progDockTile(max, num) delay 0.1 end repeat #アイコンを元に戻す current application’s NSApp’s setApplicationIconImage:(current application’s NSImage’s imageNamed:"NSApplicationIcon") end run #Dockアイコンにプログレスバーを追加 on progDockTile(max, current) set appIcon to current application’s NSImage’s imageNamed:"NSApplicationIcon" set iconSize to appIcon’s |size|() tell (current application’s NSImage’s alloc()’s initWithSize:iconSize) lockFocus() appIcon’s dissolveToPoint:(current application’s NSZeroPoint) fraction:1.0 set n to (iconSize’s width) / 16 #プログレスバーの長方形 set myRect to current application’s NSMakeRect(n / 2, n, n * 15, n * 1.6) –>{origin:{x:4.0, y:8.0}, |size|:{width:120.0, height:12.800000190735}} tell (current application’s NSBezierPath’s ¬ bezierPathWithRoundedRect:myRect ¬ xRadius:(myRect’s |size|’s height) / 2 ¬ yRadius:(myRect’s |size|’s height) / 2) current application’s (NSColor’s colorWithWhite:1.0 alpha:0.4)’s |set|() –>背景色 fill() current application’s NSColor’s whiteColor()’s |set|() –>枠色 stroke() end tell if current is greater than 0 then if current is greater than max then set current to max set myRect’s |size|’s width to (myRect’s |size|’s width) / max * current tell (current application’s NSBezierPath’s ¬ bezierPathWithRoundedRect:myRect ¬ xRadius:(myRect’s |size|’s height) / 2 ¬ yRadius:(myRect’s |size|’s height) / 2) set strartColor to current application’s NSColor’s colorWithRed:0.15 green:0.55 blue:1 alpha:0.8 set endColor to strartColor’s shadowWithLevel:0.7 set grad to current application’s NSGradient’s alloc()’s initWithStartingColor:strartColor endingColor:endColor grad’s drawInBezierPath:it angle:270.0 end tell end if unlockFocus() current application’s NSApp’s setApplicationIconImage:it end tell return (current + 1) end progDockTile |
アプリケーションのDockアイコンに文字列をバッジ表示(5文字まで)
AppleScript名:アプリケーションのDockアイコンに文字列をバッジ表示(5文字まで) |
use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" –初版では入っておらず、環境によってはクラッシュした。後から追記 showDockBadge_("") delay 1 showDockBadge_("77777") –Max 5 文字 delay 5 showDockBadge_("") –Dockのアプリケーションアイコンに指定文字をバッジ表示 on showDockBadge:theText set theDockTile to current application’s NSApp’s dockTile() theDockTile’s setBadgeLabel:theText theDockTile’s display() end showDockBadge: |
Dockアイコンをバウンドさせる
対象のアプリケーションが最前面にいるとDock上でアイコンがバウンドしないので、強制的に他のアプリケーション(Finder)を最前面に出してから実行しています。
AppleScript名:Dockアイコンをバウンドさせる |
— Created 2015-09-08 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" tell application "Finder" to activate –Script Editor/ASObjC Explorer 4を背面に set anApp to current application’s NSApplication’s sharedApplication() anApp’s requestUserAttention:(current application’s NSCriticalRequest) |
指定ファイルからカスタムアイコンを削除する
AppleScript名:指定ファイルからカスタムアイコンを削除する |
— Created 2015-10-19 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" set aFile to POSIX path of (choose file with prompt "Choose Target File") –設定対象のファイル set aSW to current application’s NSWorkspace’s sharedWorkspace() aSW’s setIcon:(missing value) forFile:aFile options:0 |
指定ファイルに指定アイコン画像をつける
AppleScript名:指定ファイルに指定アイコン画像をつける |
— Created 2015-10-19 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" set aPath to POSIX path of (choose file of type {"com.apple.icns", "public.tiff"} with prompt "Choose Icon File") –アイコンファイル set aFile to POSIX path of (choose file with prompt "Choose Target File") –設定対象のファイル set aURL to current application’s |NSURL|’s fileURLWithPath:aPath set aImage to current application’s NSImage’s alloc()’s initWithContentsOfURL:aURL set aSW to current application’s NSWorkspace’s sharedWorkspace() aSW’s setIcon:aImage forFile:aFile options:0 |
指定フォルダからカスタムアイコンを削除する v3
指定のフォルダに指定されていたアイコン画像を削除するAppleScriptです。
特定のフォルダを監視して、ファイルが追加されたり削除されたりすると、指定のAppleScriptを実行する仕組み「フォルダアクション」がmacOSに標準装備されています。
フォルダアクションはフォルダアクションでいいのですが、あまり融通が効かないので、フォルダアクションを使わずにAppleScript独自でフォルダを監視することはよくあります。
そして、監視対象に指定したフォルダのアイコンを変更し、わかりやすく「監視対象である」ことをユーザーに伝えることも、よくある話です。そして、監視処理が終了したあとでフォルダのアイコンをOS標準の元のものに戻しておく必要があります。本Scriptはそういう処理に用いるものです。
AppleScript名:指定フォルダからカスタムアイコンを削除する v3 |
— Created 2015-10-21by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" set aFolder to POSIX path of (choose folder with prompt "Choose Target Folder") –カスタムアイコン削除対象のフォルダ removeCustomIcon(aFolder) of me on removeCustomIcon(aFolder) set aSW to current application’s NSWorkspace’s sharedWorkspace() aSW’s setIcon:(missing value) forFile:aFolder options:0 –Erase tell application "Finder" update ((POSIX file aFolder) as alias) –Refresh State end tell end removeCustomIcon |
指定フォルダに指定アイコン画像をつける v3
指定のフォルダに指定アイコン画像をつけるAppleScriptです。
特定のフォルダを監視して、ファイルが追加されたり削除されたりすると、指定のAppleScriptを実行する仕組み「フォルダアクション」がmacOSに標準装備されています。
フォルダアクションはフォルダアクションでいいのですが、あまり融通が効かないので、フォルダアクションを使わずにAppleScript独自でフォルダを監視することはよくあります。
そして、監視対象に指定したフォルダのアイコンを変更し、わかりやすく「監視対象である」ことをユーザーに伝えることも、よくある話です。本Scriptはそういう処理に用いるものです。
もちろん、処理が終了したあとは監視対象フォルダのアイコンは元に戻しておくべきで、「指定フォルダからカスタムアイコンを削除する v3」とペアで使っています。
AppleScript名:指定フォルダに指定アイコン画像をつける v3 |
— Created 2015-10-21 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" set anIconPath to POSIX path of (choose file of type {"com.apple.icns", "public.tiff"} with prompt "Choose Icon File") set aFolder to POSIX path of (choose folder with prompt "Choose Folder") setCustomIcon(anIconPath, aFolder) of me on setCustomIcon(aPath, aFolder) set aFolderPath to current application’s NSString’s stringWithString:aFolder set aURL to current application’s |NSURL|’s fileURLWithPath:aPath set aImage to current application’s NSImage’s alloc()’s initWithContentsOfURL:aURL set aSW to current application’s NSWorkspace’s sharedWorkspace() aSW’s setIcon:(missing value) forFile:aFolder options:0 –Erase tell application "Finder" update ((POSIX file aFolder) as alias) –Refresh State end tell aSW’s setIcon:aImage forFile:aFolderPath options:0 –Write tell application "Finder" update ((POSIX file aFolder) as alias) –Refresh State end tell end setCustomIcon |
指定ファイルのxattrの削除(ダウンロードしたファイルが開けないときに)
AppleScript名:指定ファイルのxattrの削除(ダウンロードしたファイルが開けないときに) |
use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "XAttribute" –https://github.com/rylio/OTMXAttribute set dlFullPath to POSIX path of (choose file) set xRes to removeXAttrFromFile(dlFullPath, "com.apple.quarantine") on removeXAttrFromFile(aFile, anXattr) –Get Xattr String set anAttribute to (current application’s OTMXAttribute’s stringAttributeAtPath:aFile |name|:anXattr |error|:(missing value)) if anAttribute = missing value then return true –There is no use to remove xattr –Remove Xattr set xRes to (current application’s OTMXAttribute’s removeAttributeAtPath:aFile |name|:anXattr |error|:(missing value)) if xRes = missing value then return false return (xRes as boolean) end removeXAttrFromFile |
簡易形態素解析してスーパー大辞林で辞書検索
「words of」によるAppleScriptビルトインの超簡易形態素解析を実行して、日本語の文を単語(形態素)ごとに分解し、macOS標準搭載の辞書.appの「スーパー大辞林」で全単語を検索するAppleScriptです。
実行にあたっては、上記Frameworkを~/Library/Frameworksにインストールしたうえで、macOS 10.14以降ではScript Debuggerを用いるか、お使いのMacをSIP解除してScript Editor上で呼び出して実行する必要があります。
処理対象:”大きな栗の木の下で”
実行結果:
–> {{dictName:”スーパー大辞林”, keywordName:”で”, dictContents:”で 「て」の濁音の仮名。歯茎破裂音の有声子音と前舌の半狭母音とから成る音節。”}….}}
AppleScript名:簡易形態素解析してスーパー大辞林で辞書検索 |
— Created 2015-10-25 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "dictKit" –https://github.com/mattt/DictionaryKit set aText to "大きな栗の木の下で" set wList to words of aText repeat with i in wList set dRes to getWordDifinitionInJapaneseDictionary(i as string) of me end repeat on getWordDifinitionInJapaneseDictionary(aTerm as string) set localNameList to getNameOfLocalDictionaries() of me set dNameList to {"スーパー大辞林"} set aSet to current application’s NSMutableSet’s setWithArray:localNameList set bSet to current application’s NSMutableSet’s setWithArray:dNameList aSet’s intersectSet:bSet set dList to aSet’s allObjects() as list if dList = {} then return false set aResList to {} repeat with i in dNameList set aDictionary to (current application’s TTTDictionary’s dictionaryNamed:i) set hitEntryList to (aDictionary’s entriesForSearchTerm:aTerm) as list if hitEntryList is not equal to {missing value} then repeat with ii in hitEntryList set j to contents of ii set headW to (j’s headword) set headW to headW as text try set aText to (j’s |text|) set aText to aText as text on error set aText to (j’s HTML) set aText to decodeCharacterReference(aText) of me end try set the end of aResList to {dictName:(i as text), keywordName:headW, dictContents:aText} end repeat end if end repeat aResList end getWordDifinitionInJapaneseDictionary on decodeCharacterReference(aStr) set anNSString to current application’s NSString’s stringWithString:aStr set theData to anNSString’s dataUsingEncoding:(current application’s NSUTF16StringEncoding) set styledString to current application’s NSAttributedString’s alloc()’s initWithHTML:theData documentAttributes:(missing value) set plainText to (styledString’s |string|()) as string return plainText end decodeCharacterReference on getNameOfLocalDictionaries() set dSet to current application’s TTTDictionary’s availableDictionaries() set dList to dSet’s allObjects() set dNameList to {} repeat with i in dList set the end of dNameList to (i’s |name|()) as text end repeat return dNameList end getNameOfLocalDictionaries |
Dictionary.appの辞書名を言語で抽出
AppleScript名:DIctionary.appの辞書名を言語で抽出 |
— Created 2015-10-25 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "dictKit" –https://github.com/mattt/DictionaryKit –Dictionary.appのすべての辞書名を出力 set d1Res to getEveryLocalDictionaryInformation() of me –> {{dictName:"뉴에이스 영한사전 / 뉴에이스 한영사전", dictLang:"韓国語 – 英語"}, {dictName:"Multidictionnaire de la langue française", dictLang:"フランス語"}, {dictName:"राजपाल हिन्दी शब्दकोश", dictLang:"ヒンディー語"}, ….} –DIctionary.appの辞書のうち言語ラベル(shortName)が「日本語」ではじまるものを抽出(和英辞典など) set d2Res to getLocalDictionaryInformationByFromLang("日本語") of me –> {{dictName:"スーパー大辞林", dictLang:"日本語"}, {dictName:"ウィズダム英和辞典 / ウィズダム和英辞典", dictLang:"日本語 – 英語"}} –Dictionary.appの辞書のうち言語ラベル(shortName)が「日本語」で終わるものを抽出(英和辞典、日本語辞典) set d3Res to getLocalDictionaryInformationByToLang("日本語") of me –> {{dictName:"スーパー大辞林", dictLang:"日本語"}} –Dictionary.appの辞書のうち言語ラベル(shortName)が「日本語」ではじまり「英語」で終わるもの(和英辞典)を抽出 set d4Res to getLocalDictionaryInformationByFromToLang("日本語", "英語") of me –> {{dictName:"ウィズダム英和辞典 / ウィズダム和英辞典", dictLang:"日本語 – 英語"}} on getEveryLocalDictionaryInformation() set dSet to current application’s TTTDictionary’s availableDictionaries() set dList to dSet’s allObjects() set dNameList to {} repeat with i in dList set aName to (|name|() of i) as string set sName to (shortName() of i) as string set the end of dNameList to {dictName:aName, dictLang:sName} end repeat return dNameList end getEveryLocalDictionaryInformation on getLocalDictionaryInformationByFromLang(aFromLang) set dSet to current application’s TTTDictionary’s availableDictionaries() set dList to dSet’s allObjects() set dNameList to {} repeat with i in dList set aName to (|name|() of i) as string set sName to (shortName() of i) as string if sName begins with aFromLang then set the end of dNameList to {dictName:aName, dictLang:sName} end if end repeat return dNameList end getLocalDictionaryInformationByFromLang on getLocalDictionaryInformationByToLang(aToLang) set dSet to current application’s TTTDictionary’s availableDictionaries() set dList to dSet’s allObjects() set dNameList to {} repeat with i in dList set aName to (|name|() of i) as string set sName to (shortName() of i) as string if sName ends with aToLang then set the end of dNameList to {dictName:aName, dictLang:sName} end if end repeat return dNameList end getLocalDictionaryInformationByToLang on getLocalDictionaryInformationByFromToLang(aFromLang, aToLang) set dSet to current application’s TTTDictionary’s availableDictionaries() set dList to dSet’s allObjects() set dNameList to {} repeat with i in dList set aName to (|name|() of i) as string set sName to (shortName() of i) as string if sName starts with aFromLang and sName ends with aToLang then set the end of dNameList to {dictName:aName, dictLang:sName} end if end repeat return dNameList end getLocalDictionaryInformationByFromToLang |
指定辞書で指定キーワードを串刺し検索
AppleScript名:指定辞書で指定キーワードを串刺し検索 |
— Created 2015-10-25 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "dictKit" –https://github.com/mattt/DictionaryKit set dRes to getWordDifinitionInDictionaries("青い", {"スーパー大辞林"}) of me on getWordDifinitionInDictionaries(aTerm as string, dNameList as list) set localNameList to getNameOfLocalDictionaries() of me –指定辞書がローカル環境に存在しているかどうかチェック set aSet to current application’s NSMutableSet’s setWithArray:localNameList set bSet to current application’s NSMutableSet’s setWithArray:dNameList aSet’s intersectSet:bSet set dList to aSet’s allObjects() as list if dList = {} then return false set aResList to {} repeat with i in dNameList set aDictionary to (current application’s TTTDictionary’s dictionaryNamed:i) set hitEntryList to (aDictionary’s entriesForSearchTerm:aTerm) as list if hitEntryList is not equal to {missing value} then repeat with ii in hitEntryList set j to contents of ii set headW to (j’s headword) set headW to headW as text try set aText to (j’s |text|) set aText to aText as text on error set aText to (j’s HTML) set aText to decodeCharacterReference(aText) of me end try set the end of aResList to {dictName:(i as text), keywordName:headW, dictContents:aText} end repeat end if end repeat aResList end getWordDifinitionInDictionaries on decodeCharacterReference(aStr) set anNSString to current application’s NSString’s stringWithString:aStr set theData to anNSString’s dataUsingEncoding:(current application’s NSUTF16StringEncoding) set styledString to current application’s NSAttributedString’s alloc()’s initWithHTML:theData documentAttributes:(missing value) set plainText to (styledString’s |string|()) as string return plainText end decodeCharacterReference on getNameOfLocalDictionaries() set dSet to current application’s TTTDictionary’s availableDictionaries() set dList to dSet’s allObjects() set dNameList to {} repeat with i in dList set the end of dNameList to (i’s |name|()) as text end repeat return dNameList end getNameOfLocalDictionaries |
ASOCでOS内蔵辞書を串刺し検索するじっけん3
AppleScript名:ASOCでOS内蔵辞書を串刺し検索するじっけん3 |
— Created 2015-10-25 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "dictKit" –https://github.com/mattt/DictionaryKit –Japanese & English Dictionaries set dNameList to {"Apple用語辞典", "Oxford Thesaurus of English", "スーパー大辞林", "Oxford Dictionary of English", "Oxford American Writer’s Thesaurus", "Wikipedia", "New Oxford American Dictionary", "ウィズダム英和辞典 / ウィズダム和英辞典", "Wikipedia"} –English Dictionaries set dNameList to {"Oxford American Writer’s Thesaurus", "New Oxford American Dictionary"} –Wikipedia set dNameList to {"Wikipedia"} set aTerm to "set" set aResList to {} repeat with i in dNameList set aDictionary to (current application’s TTTDictionary’s dictionaryNamed:i) set hitEntryList to (aDictionary’s entriesForSearchTerm:aTerm) as list if hitEntryList is not equal to {missing value} then repeat with ii in hitEntryList set j to contents of ii set headW to (j’s headword) set headW to headW as text try set aText to (j’s |text|) set aText to aText as text on error set aText to (j’s HTML) set aText to decodeCharacterReference(aText) of me end try set the end of aResList to {dictName:(i as text), keywordName:headW, dictContents:aText} end repeat end if end repeat aResList on decodeCharacterReference(aStr) set anNSString to current application’s NSString’s stringWithString:aStr set theData to anNSString’s dataUsingEncoding:(current application’s NSUTF16StringEncoding) set styledString to current application’s NSAttributedString’s alloc()’s initWithHTML:theData documentAttributes:(missing value) set plainText to (styledString’s |string|()) as string return plainText end decodeCharacterReference |
指定辞書でキーワード検索
AppleScript名:指定辞書でキーワード検索 |
— Created 2017-12-30 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "dictKit" –https://github.com/mattt/DictionaryKit set aRes to findKeywordWithDictionaryApp("rake", "ウィズダム英和辞典 / ウィズダム和英辞典") of me on findKeywordWithDictionaryApp(aKeyword, aDictName) set aDictionary to (current application’s TTTDictionary’s dictionaryNamed:aDictName) set hitEntryList to (aDictionary’s entriesForSearchTerm:aKeyword) as list if hitEntryList is not equal to {missing value} then repeat with ii in hitEntryList set j to contents of ii set headW to (j’s headword) set headW to headW as text try set aText to (j’s |text|) set aText to aText as text on error set aText to (j’s HTML) set aText to decodeCharacterReference(aText) of me end try return aText end repeat return {} end if end findKeywordWithDictionaryApp on decodeCharacterReference(aStr) set anNSString to current application’s NSString’s stringWithString:aStr set theData to anNSString’s dataUsingEncoding:(current application’s NSUTF16StringEncoding) set styledString to current application’s NSAttributedString’s alloc()’s initWithHTML:theData documentAttributes:(missing value) set plainText to (styledString’s |string|()) as string return plainText end decodeCharacterReference |
日本語の慣用句を検索する
辞書.app内の辞書「スーパー大辞林」を対象に、慣用句のキーワード検索(部分一致検索)を行うAppleScriptです。
–> dictKit.framework (To ~/Library/Frameworks/)
掲載時はmacOS 10.12上で作成、検証を行っていましたが、その後macOS 10.14でホームディレクトリ下のFrameworkへのアクセスが禁止されたため、実行にはScript Debugger上、およびScript Debuggerから書き出したEnhanced AppleScript Applet上で書き出して実行する必要があります。
# あるいは、SIPを解除すればスクリプトエディタ上でも実行可能です
AppleScript名:日本語の慣用句を検索する |
— Created 2017-12-30 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "dictKit" –https://github.com/mattt/DictionaryKit set aRes to retJapaneseIdionFromKanjiChar("血") of me –> {"血が通う", "血が騒ぐ", "血が繫がる", "血が上る", "血が引く", "血で血を洗う", "血と汗", "血となり肉となる", "血に飢える", "血の出るよう", "血の滲むよう", "血は争えない", "血は水よりも濃い", "血も涙もない", "血湧き肉躍る", "血を受ける", "血を歃る", "血を吐く思い", "血を引く", "血を見る", "血を分ける"} set aRes to retJapaneseIdionFromKanjiChar("家") of me –> {"家給し人足る", "家高し", "家に杖つく", "家貧しくして孝子顕わる", "家をあける", "家を出ず", "家を外にする"} set aRes to retJapaneseIdionFromKanjiChar("水") of me –> {"水到りて渠成る", "水が合わない", "水が漬く", "水が入る", "水が引く", "水涸る", "水清ければ魚棲まず", "水澄む", "水で割る", "水と油", "水にする", "水に流す", "水になる", "水に馴れる", "水温む", "水の滴るよう", "水の流れと身のゆくえ", "水の低きに就く如し", "水は方円の器に随う", "水も漏らさぬ", "水をあける", "水を打ったよう", "水を得た魚のよう", "水を掛ける", "水をさす", "水を向ける"} set aRes to retJapaneseIdionFromKanjiChar("木") of me –> {"木から落ちた猿", "樹静かならんと欲すれども風止まず", "木で鼻を括る", "木に竹を接ぐ", "木にも草にも心を置く", "木に餅がなる", "木に縁りて魚を求む", "木の股から生まれる", "木六竹八塀十郎", "木を見て森を見ず"} on retJapaneseIdionFromKanjiChar(aKanji) set aDictionary to (current application’s TTTDictionary’s dictionaryNamed:"スーパー大辞林") set hitEntryList to (aDictionary’s entriesForSearchTerm:aKanji) as list if hitEntryList is not equal to {missing value} then repeat with ii in hitEntryList set j to contents of ii set headW to (j’s headword) set headW to headW as text try set aText to (j’s |text|) set aText to aText as text on error set aText to (j’s HTML) set aText to decodeCharacterReference(aText) of me end try if aText contains "〈句項目〉" then set aCount to 1 set tmpList to paragraphs of aText set aLen to length of tmpList repeat with i in tmpList set j to contents of i if j contains "〈句項目〉" then set outList to items (aCount + 1) thru -1 of tmpList exit repeat end if set aCount to aCount + 1 end repeat repeat with ii from (aCount + 1) to aLen set jj to contents of ii if jj = "" then exit repeat end repeat set outList to contents of items (aCount + 1) thru (ii – 1) of tmpList return outList end if end repeat return {} end if end retJapaneseIdionFromKanjiChar on decodeCharacterReference(aStr) set anNSString to current application’s NSString’s stringWithString:aStr set theData to anNSString’s dataUsingEncoding:(current application’s NSUTF16StringEncoding) set styledString to current application’s NSAttributedString’s alloc()’s initWithHTML:theData documentAttributes:(missing value) set plainText to (styledString’s |string|()) as string return plainText end decodeCharacterReference |