AppleScript名:OLD Style ASでチルダ入りパス展開 |
— Created 2016-05-13 by Christopher Stone
set aPath to "~/Desktop" set aRes to expandTildeInPath(aPath) of me –> "/Users/me/Desktop" on expandTildeInPath(thePath) tell application "System Events" return (POSIX path of (disk item thePath)) end tell end expandTildeInPath |
タグ: 10.13savvy
チルダ入りパス展開
チルダ入りのPOSIX pathをCocoaの機能を用いてフルパスに展開するAppleScriptです。
AppleScript名:チルダ入りパス展開 |
use AppleScript version "2.4" use scripting additions use framework "Foundation" set bPath to "~/Desktop" set pathString to current application’s NSString’s stringWithString:bPath set newPath to pathString’s stringByExpandingTildeInPath() as string –> "/Users/me/Desktop" |
フルパスからファイル名を取得する
フルパスからファイル名を取得するAppleScriptです。
AppleScriptでは、ファイルパス形式をいくつか併用しています。HFS形式、URL形式、POSIX形式などです。
▲2019/7/20変更。POSIX pathからaliasに直接変換できるような誤解を招く表現があったので、いったんPOSIX file(=file)を経由しないとaliasに変換できないことを表現
▲2022/6/3変更。NSURL(file://)から直接「as alias」で変換できることが判明。この変換を追記
ここではPOSIX形式のパスからファイル名を、Cocoaの機能を用いて求めるScriptを掲載しています。
AppleScript名:フルパスからファイル名を取得する |
— Created 2017-02-04 10:38:44 +0900 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set aPath to "/Users/me/Documents/ぴよまるソフトウェア/–Book1「AppleScript最新リファレンス」/9999_images/E81F91B7-4931-463F-A027-21A18A853290.jpg" set aStr to (current application’s NSString’s stringWithString:aPath)’s lastPathComponent() as string –> "E81F91B7-4931-463F-A027-21A18A853290.jpg" |
古典的なAppleScriptのやり方だと、aliasからアプリケーションの機能を用いてファイル名を取得することになります。
AppleScript名:aliasからファイル名を取得(Finder) |
set aFile to choose file
tell application "Finder" set aName to name of aFile end tell |
AppleScript名:aliasからファイル名を取得(System Events) |
set aFile to choose file
tell application "System Events" set aName to name of aFile end tell |
フルパスからファイル名を取得して拡張子を削除する
AppleScript名:フルパスからファイル名を取得して拡張子を削除する |
— Created 2017-02-04 10:38:44 +0900 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set aPath to "/Users/me/Documents/ぴよまるソフトウェア/–Book1「AppleScript最新リファレンス」/9999_images/E81F91B7-4931-463F-A027-21A18A853290.jpg" set aStr to (current application’s NSString’s stringWithString:aPath)’s lastPathComponent()’s stringByDeletingPathExtension() as string –> "E81F91B7-4931-463F-A027-21A18A853290.jpg" |
ファイルパスの変換(Alias→POSIX path→NSURL→POSIX path→file→Alias)
AppleScriptで使用するファイルパスには、alias(hfs形式)、file(hfs形式)、POSIX path形式(shell commandとのやりとりに使う)、URL形式(ほとんど使わないがごくまれに)などがありますが、macOS 10.10以降はCocoaのオブジェクトを扱えるようになったので、Cocoaオブジェクトとの間でも変換する必要が出てきました。
CocoaもPath形式にPOSIX path(NSString)、URL(NSURL)という複数のパス表現形式があり、AppleScript側のファイルパス情報と相互変換する必要があります。相互変換が一発でできれば楽ですが、たとえばaliasならalias→POSIX path→CocoaのNSStringのpathと2段階で変換する必要があります。
難しいかといわれれば、別に慣れればそんなもんでしょう。技術的な難易度はそれほど高くありません。こうした全体像を知っているかどうか、ということがキーです。
AppleScript名:ファイルパスの変換(Alias→POSIX path→NSURL→POSIX path→file→Alias) |
— Created 2016-02-19 by Takaaki Naganoya — 2016 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" –Get Alias (AppleScript native file path format) set anAlias to choose file –> alias "Macintosh HD:Users:me:Documents:……" –Get POSIX path from Alias set aPOSIX to POSIX path of anAlias –> "/Users/me/Documents/……" –Get NSURL from POSIX path set aURL to current application’s |NSURL|’s fileURLWithPath:aPOSIX –> (NSURL) file:///Users/me/Documents/………%83%88%EF%BC%92.scptd/ –Get NSURL string set bPath to aURL’s absoluteString() –> (NSString) "file:///Users/me/Documents/A….9C%EF%BC%89.scptd/" –Get file path (POSIX path) NSString set cPath to aURL’s |path|() –> (NSString) "/Users/me/Documents/……scptd" –Get POSIX file path string from NSString set dPath to cPath as string –> "/Users/me/Documents/……..scptd" –Get file from POSIX path set aFile to POSIX file dPath –> file "Macintosh HD:Users:me:Documents:….scptd" –Get Alias from file set bAlias to aFile as alias –> alias "Macintosh HD:Users:me:Documents:….scptd" |
POSIX –> URL –> POSIX
AppleScript名:POSIX –> URL –> POSIX |
— Created 2017-03-02 23:41:25 +0900 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set aPOSIX to POSIX path of (choose file) –POSIX set aURL to current application’s |NSURL|’s fileURLWithPath:aPOSIX –URL set bPath to aURL’s |path|() as string –POSIX |
file URL String to alias
AppleScript名:file URL String to alias |
— Created 2017-09-18 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" property |NSURL| : a reference to current application’s |NSURL| set aFile to choose file set aPOSIX to POSIX path of aFile set aFileURL to (|NSURL|’s fileURLWithPath:aPOSIX)’s absoluteString() as string –Target (file URL string) –> "file:///Users/maro/Documents/…. set aPath to ((|NSURL|’s URLWithString:aFileURL)’s |path|()) as string set bPath to (POSIX file aPath) as alias –> alias "Cherry:Users:maro:Documents/… |
相対パスから絶対パスを計算してを求める v2
AppleScript名:相対パスから絶対パスを計算してを求める v2 |
— Created 2017-11-11 by Takaaki Naganoya use AppleScript version "2.4" use scripting additions use framework "Foundation" set absolutePath to "/Users/me/Documents/–Book 1「AppleScript最新リファレンス」/5000 iOSデバイスとの連携/5100 iOSデバイスからMacに画面を出力するAirServer.md" set relativePath to "../9999_images/img-1.jpg" set relativePath to calcAbsolutePath(absolutePath, relativePath) of me –> "/Users/me/Documents/–Book 1「AppleScript最新リファレンス」/9999_images/img-1.jpg" on calcAbsolutePath(aAbsolutePOSIXfile, bRelativePOSIXfile) set aStr to current application’s NSString’s stringWithString:aAbsolutePOSIXfile set bStr to current application’s NSString’s stringWithString:bRelativePOSIXfile set aList to aStr’s pathComponents() as list set bList to bStr’s pathComponents() as list set aLen to length of aList set aCount to 1 repeat with i in bList set j to contents of i if j is not equal to ".." then exit repeat end if set aCount to aCount + 1 end repeat set tmp1List to items 1 thru (aLen – aCount) of aList set tmp2List to items aCount thru -1 of bList set allRes to current application’s NSString’s pathWithComponents:(tmp1List & tmp2List) return allRes as text end calcAbsolutePath |
2つのパスの相対パスを求める v3
AppleScript名:2つのパスの相対パスを求める v3 |
— Created 2017-01-28 by Takaaki Naganoya — Modified 2017-01-30 by Shane Stanley — Modified 2017-02-03 by Takaaki Naganoya use AppleScript version "2.4" use scripting additions use framework "Foundation" –Markdown書類(フルパス) set aFile to "/Users/me/Documents/ぴよまるソフトウェア/書籍原稿/–Book 1a「AppleScript最新リファレンス バージョン2.7対応」/5000 iOSデバイスとの連携/5100 iOSデバイスからMacに画面を出力するAirServer.md" –リンク画像(フルパス) set bFile to "/Users/me/Documents/ぴよまるソフトウェア/書籍原稿/–Book 1a「AppleScript最新リファレンス バージョン2.7対応」/9999_images/img-1.php.jpeg" set relativePath to calcRelativePath(aFile, bFile) of me –> "../9999_images/img-1.php.jpeg" on calcRelativePath(aPOSIXfile, bPOSIXfile) set aStr to current application’s NSString’s stringWithString:aPOSIXfile set bStr to current application’s NSString’s stringWithString:bPOSIXfile set aList to aStr’s pathComponents() as list set bList to bStr’s pathComponents() as list set aLen to length of aList set bLen to length of bList if aLen ≥ bLen then copy aLen to aMax else copy bLen to aMax end if repeat with i from 1 to aMax set aTmp to contents of item i of aList set bTmp to contents of item i of bList if aTmp is not equal to bTmp then exit repeat end if end repeat set bbList to items i thru -1 of bList set aaItem to (length of aList) – i set tmpStr to {} repeat with ii from 1 to aaItem set the end of tmpStr to ".." end repeat set allRes to current application’s NSString’s pathWithComponents:(tmpStr & bbList) return allRes as text end calcRelativePath |
Bluetoothに接続中のデバイス名を取得するv4
AppleScript名:Bluetoothに接続中のデバイス名を取得するv4 |
— Created 2017-07-24 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "IOBluetooth" set pRes to getBluetoothPowerState() of me if pRes = false then return set dArray to current application’s IOBluetoothDevice’s pairedDevices() set aRes to my filterRecListByLabel1(dArray, "mIONotification != 0") set dNames to (aRes’s mName) as list –> {"Piyomaru AirPods", "Takaaki Naganoya のマウス"} –リストに入れたレコードを、指定の属性ラベルの値で抽出 on filterRecListByLabel1(aRecList, aPredicate as string) set aArray to current application’s NSArray’s arrayWithArray:aRecList set aPredicate to current application’s NSPredicate’s predicateWithFormat:aPredicate set filteredArray to aArray’s filteredArrayUsingPredicate:aPredicate return filteredArray end filterRecListByLabel1 –Mac本体のBluetoothのパワー状態を取得 on getBluetoothPowerState() set aCon to current application’s IOBluetoothHostController’s alloc()’s init() set pRes to (aCon’s powerState()) as boolean end getBluetoothPowerState |
Bluetoothのオン、オフ状態を取得する
AppleScript名:Bluetoothのオン、オフ状態を取得する |
— Created 2017-07-25 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "IOBluetooth" set pRes to getBluetoothPowerState() of me on getBluetoothPowerState() set aCon to current application’s IOBluetoothHostController’s alloc()’s init() set pRes to (aCon’s powerState()) as boolean end getBluetoothPowerState |
Bluetoothに接続中のデバイス名を取得、AirPodsを切断する
AppleScript名:Bluetoothに接続中のデバイス名を取得、AirPodsを切断する |
— Created 2017-08-05 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.5" –macOS 10.11 or later use scripting additions use framework "Foundation" use framework "IOBluetooth" –参照 https://github.com/lapfelix/BluetoothConnector property IOBluetoothHostController : a reference to current application’s IOBluetoothHostController property IOBluetoothDevice : a reference to current application’s IOBluetoothDevice set dList to getActiveBluetoothDevices() of me –> {{deviceName:"Piyomaru AirPods", deviceAddress:"Xx-XX-xX-Xx-xx-xx"}, {deviceName:"Takaaki Naganoya のマウス", deviceAddress:"xx-xx-XX-xx-XX-Xx"}, {deviceName:"Takaaki Naganoya のキーボード #1", deviceAddress:"XX-XX-xX-xx-Xx-xX"}} set dRes to filterRecListByLabel(dList, "deviceName contains ’AirPods’") of me if dRes = {} then return false –Case: No match set dAddr to dRes’s first item’s deviceAddress set cnRes1 to disconnectBluetoothDeviceByAddress(dAddr) of me –> true (Successfully Disconnected) delay 10 set cnRes2 to connectBluetoothDeviceByAddress(dAddr) of me –> true (Successfully Connected) –指定アドレスのBluetooth Deviceを接続する on connectBluetoothDeviceByAddress(addressStr as string) if getBluetoothPowerState() = false then error "Bluetooth Power is not active with your Mac" set aBTList to IOBluetoothDevice’s pairedDevices() as list repeat with i in aBTList set aClass to (current application’s NSStringFromClass(i’s |class|())) as string if aClass is equal to "IOBluetoothDevice" then set anAddress to i’s addressString() as string if anAddress = addressStr then i’s openConnection() return true end if end if end repeat return false end connectBluetoothDeviceByAddress –指定アドレスのBluetooth Deviceの接続を切る on disconnectBluetoothDeviceByAddress(addressStr as string) if getBluetoothPowerState() = false then error "Bluetooth Power is not active with your Mac" set aBTList to IOBluetoothDevice’s pairedDevices() as list repeat with i in aBTList set aClass to (current application’s NSStringFromClass(i’s |class|())) as string if aClass is equal to "IOBluetoothDevice" then set anAddress to i’s addressString() as string if anAddress = addressStr then i’s closeConnection() return true end if end if end repeat return false end disconnectBluetoothDeviceByAddress –ペアリング済みのBluetooth Deviceの情報を取得 on getActiveBluetoothDevices() if getBluetoothPowerState() = false then error "Bluetooth Power is not active with your Mac" set aBTList to IOBluetoothDevice’s pairedDevices() as list set devList to {} repeat with i in aBTList set aClass to (current application’s NSStringFromClass(i’s |class|())) as string if aClass = "IOBluetoothDevice" then set aName to i’s |name|() as string set anAddress to i’s addressString() as string set aPaired to i’s isPaired() as boolean –set aConnect to i’s isConnected() as boolean if aPaired = true then set the end of devList to {deviceName:aName, deviceAddress:anAddress} end if end if end repeat return devList end getActiveBluetoothDevices –Mac本体のBluetoothのパワー状態を取得 on getBluetoothPowerState() set aCon to IOBluetoothHostController’s alloc()’s init() set pRes to (aCon’s powerState()) as boolean end getBluetoothPowerState –リストに入れたレコードを、指定の属性ラベルの値で抽出 on filterRecListByLabel(aRecList as list, aPredicate as string) set aArray to current application’s NSArray’s arrayWithArray:aRecList set aPredicate to current application’s NSPredicate’s predicateWithFormat:aPredicate set filteredArray to aArray’s filteredArrayUsingPredicate:aPredicate set bList to filteredArray as list return bList end filterRecListByLabel |
Bluetoothに接続中のデバイスをメーカー名とマイナー種別で抽出
AppleScript名:Bluetoothに接続中のデバイスをメーカー名とマイナー種別で抽出 |
— Created 2017-08-06 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" property NSArray : a reference to current application’s NSArray property NSString : a reference to current application’s NSString property NSPredicate : a reference to current application’s NSPredicate property NSMutableArray : a reference to current application’s NSMutableArray property NSMutableDictionary : a reference to current application’s NSMutableDictionary property NSPropertyListFormat : a reference to current application’s NSPropertyListFormat property NSUTF8StringEncoding : a reference to current application’s NSUTF8StringEncoding property NSPropertyListImmutable : a reference to current application’s NSPropertyListImmutable property NSPropertyListSerialization : a reference to current application’s NSPropertyListSerialization –製造者が"Apple"のBluetoothデバイスをリストアップ set qRes to returnBTPeripheral from "Apple" –> {{device_supportsESCO:"attrib_Yes", device_role:"attrib_master", device_manufacturer:"Apple (0x6, 0x03)", device_services:"Handsfree, Wireless iAP, AVRCP Controller, Audio Sink, AVRCP Target, AAP Server", device_isconnected:"attrib_Yes", device_RSSI:-51, device_majorClassOfDevice_string:"Audio", device_isconfigured:"attrib_Yes", device_minorClassOfDevice_string:"Headphones", device_interval:"441.25 ms", device_addr:"XX-XX-XX-XX-XX-XX", device_ConnectionMode:"attrib_sniff_mode", device_productID:"0x2002", device_supportsSSP:"attrib_Yes", device_classOfDevice:"0x04 0x06 0x240418", device_vendorID:"0x004C", device_fw_version:"0x0372", device_ispaired:"attrib_Yes", device_supportsEDR:"attrib_Yes"}, {device_supportsESCO:"attrib_No", device_manufacturer:"Apple (0x3, 0x31C)", device_ispaired:"attrib_Yes", device_services:"Apple Wireless Mouse", device_isconnected:"attrib_No", device_majorClassOfDevice_string:"Peripheral", device_isNormallyConnectable:"attrib_Yes", device_isconfigured:"attrib_Yes", device_addr:"XX-XX-XX-XX-XX-XX", device_productID:"0x030D", device_supportsSSP:"attrib_No", device_vendorID:"0x05AC", device_classOfDevice:"0x05 0x20 0x2580", device_minorClassOfDevice_string:"Mouse", device_fw_version:"0x0084", device_supportsEDR:"attrib_No"}} –Appleのデバイスでも製造者がAppleになっていないものもある。Magic Keyboard 2とか –種類(マイナー)が "Headphones"のBluetoothデバイスをリストアップ set qRes to returnBTPeripheral about "Headphones" –> {{device_supportsESCO:"attrib_Yes", device_role:"attrib_master", device_manufacturer:"Apple (0x6, 0x03)", device_services:"Handsfree, Wireless iAP, AVRCP Controller, Audio Sink, AVRCP Target, AAP Server", device_isconnected:"attrib_Yes", device_RSSI:-51, device_majorClassOfDevice_string:"Audio", device_isconfigured:"attrib_Yes", device_minorClassOfDevice_string:"Headphones", device_interval:"441.25 ms", device_addr:"XX-XX-XX-XX-XX-XX", device_ConnectionMode:"attrib_sniff_mode", device_productID:"0x2002", device_supportsSSP:"attrib_Yes", device_classOfDevice:"0x04 0x06 0x240418", device_vendorID:"0x004C", device_fw_version:"0x0372", device_ispaired:"attrib_Yes", device_supportsEDR:"attrib_Yes"}} –製造者が"Apple"で、種類(マイナー)が "Headphones"のBluetoothデバイスをリストアップ set qRes to returnBTPeripheral from "Apple" about "Headphones" –> {{device_supportsESCO:"attrib_Yes", device_role:"attrib_master", device_manufacturer:"Apple (0x6, 0x03)", device_services:"Handsfree, Wireless iAP, AVRCP Controller, Audio Sink, AVRCP Target, AAP Server", device_isconnected:"attrib_Yes", device_RSSI:-52, device_majorClassOfDevice_string:"Audio", device_isconfigured:"attrib_Yes", device_minorClassOfDevice_string:"Headphones", device_interval:"441.25 ms", device_addr:"XX-XX-XX-XX-XX-XX", device_ConnectionMode:"attrib_sniff_mode", device_productID:"0x2002", device_supportsSSP:"attrib_Yes", device_classOfDevice:"0x04 0x06 0x240418", device_vendorID:"0x004C", device_fw_version:"0x0372", device_ispaired:"attrib_Yes", device_supportsEDR:"attrib_Yes"}} on returnBTPeripheral from devMaker as string : "" about kindName as string : "" set sRes to do shell script "/usr/sbin/system_profiler SPBluetoothDataType -detailLevel full -xml" set aSource to (readPlistFromStr(sRes) of me) as list set aaList to contents of first item of aSource set resArray to NSMutableArray’s new() set aList to _items of aaList repeat with i in aList set aDict to (NSMutableDictionary’s dictionaryWithDictionary:(contents of i)) set aKeyList to (aDict’s allKeys()) as list set dResList to (aDict’s valueForKeyPath:"device_title") repeat with ii in dResList set dKeyList to ii’s allKeys() set dKey to first item of dKeyList set dDic to (ii’s valueForKeyPath:dKey) if devMaker is not equal to "" and kindName is not equal to "" then set qText to "device_manufacturer contains ’" & devMaker & "’ && device_minorClassOfDevice_string ==’" & kindName & "’" else if devMaker is not equal to "" then set qText to "device_manufacturer contains ’" & devMaker & "’" else if kindName is not equal to "" then set qText to "device_minorClassOfDevice_string ==’" & kindName & "’" end if set dRes to filterRecListByLabel(dDic, qText) of me if (dRes as list) is not equal to {} then (resArray’s addObject:(first item of dRes)) end if end repeat end repeat return resArray as list end returnBTPeripheral –stringのplistを読み込んでRecordに on readPlistFromStr(theString) set aSource to NSString’s stringWithString:theString set pListData to aSource’s dataUsingEncoding:(NSUTF8StringEncoding) set aPlist to NSPropertyListSerialization’s propertyListFromData:pListData mutabilityOption:(NSPropertyListImmutable) |format|:(NSPropertyListFormat) errorDescription:(missing value) return aPlist end readPlistFromStr –リストに入れたレコードを、指定の属性ラベルの値で抽出 on filterRecListByLabel(aRecList as list, aPredicate as string) set aArray to NSArray’s arrayWithArray:aRecList set aPredicate to NSPredicate’s predicateWithFormat:aPredicate set filteredArray to aArray’s filteredArrayUsingPredicate:aPredicate set bList to filteredArray as list return bList end filterRecListByLabel |
NSProcessInfoでプロセスの各種情報を取得
AppleScript名:NSProcessInfoでプロセスの各種情報を取得 |
— Created 2018-02-15 by Takaaki Naganoya — 2018 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set aInfo to current application’s NSProcessInfo’s processInfo()’s processorCount() –> 8 set aInfo to current application’s NSProcessInfo’s processInfo()’s activeProcessorCount() –> 8 set aInfo to current application’s NSProcessInfo’s processInfo()’s physicalMemory() –> 8.589934592E+9 set aInfo to current application’s NSProcessInfo’s processInfo()’s systemUptime() –> 3.55849418142903E+5 set aInfo to (current application’s NSProcessInfo’s processInfo()’s hostName()) as string –> "mbpretina.local" set aInfo to (current application’s NSProcessInfo’s processInfo()’s operatingSystemVersionString()) as string –> "バージョン10.12.6(ビルド16G1309)" set vInfo to current application’s NSProcessInfo’s processInfo()’s operatingSystemVersion() –> {majorVersion:10, minorVersion:12, patchVersion:6} set aInfo to current application’s NSProcessInfo’s processInfo()’s isOperatingSystemAtLeastVersion:vInfo –> true set aInfo to (current application’s NSProcessInfo’s processInfo()’s thermalState()) –> 1 –0: NSProcessInfoThermalStateCritical –1: NSProcessInfoThermalStateFair –2: NSProcessInfoThermalStateNominal –3: NSProcessInfoThermalStateSerious |
デスクトップ・スクリーンセーバー
ステータスバーアイテムを作成してメニューバー上にメニューを出し、デスクトップ・スクリーンセーバーのコントロールを行うAppleScriptです。
ステータスバーアイテムを使って、簡単なメニュー操作を行うAppleScriptの試作品です。メニュー操作の方がメインで、デスクトップ・スクリーンセーバーの方はオマケです。
AppleScript名:デスクトップ・スクリーンセーバー |
— Created 2017-03-03 by Takaaki Naganoya — Modified 2018-02-15 by Shane Stanley–Thanks!! — Modified 2018-02-15 by Takaaki Naganoya use AppleScript version "2.5" use scripting additions use framework "Foundation" use framework "AppKit" property aStatusItem : missing value on run init() of me end run on init() set aList to {"Desktop ScreenSaver", "", "Stop", "", "Quit"} set aStatusItem to current application’s NSStatusBar’s systemStatusBar()’s statusItemWithLength:(current application’s NSVariableStatusItemLength) aStatusItem’s setTitle:"💻" aStatusItem’s setHighlightMode:true aStatusItem’s setMenu:(createMenu(aList) of me) end init on createMenu(aList) set aMenu to current application’s NSMenu’s alloc()’s init() set aCount to 10 set prevMenuItem to "" repeat with i in aList set j to contents of i set aClass to (class of j) as string if j is equal to "" then set aMenuItem to (current application’s NSMenuItem’s separatorItem()) (aMenu’s addItem:aMenuItem) else if (aClass = "text") or (aClass = "string") then if j = "Quit" then set aMenuItem to (current application’s NSMenuItem’s alloc()’s initWithTitle:j action:"actionHandler:" keyEquivalent:"") else set aMenuItem to (current application’s NSMenuItem’s alloc()’s initWithTitle:j action:"actionHandler:" keyEquivalent:"") end if (aMenuItem’s setTag:aCount) (aMenuItem’s setTarget:me) (aMenu’s addItem:aMenuItem) set aCount to aCount + 10 copy aMenuItem to prevMenuItem else if aClass = "list" then –Generate Submenu set subMenu to current application’s NSMenu’s new() (aMenuItem’s setSubmenu:subMenu) set subCounter to 1 repeat with ii in j set jj to contents of ii set subMenuItem1 to (current application’s NSMenuItem’s alloc()’s initWithTitle:jj action:"actionHandler:" keyEquivalent:"") (subMenuItem1’s setTarget:me) (subMenuItem1’s setTag:(aCount + subCounter)) (subMenu’s addItem:subMenuItem1) set subCounter to subCounter + 1 end repeat end if end if end repeat return aMenu end createMenu on actionHandler:sender set aTag to tag of sender as string set aTitle to title of sender as string if aTitle is equal to "Quit" then current application’s NSStatusBar’s systemStatusBar()’s removeStatusItem:aStatusItem –tell me to quit else if aTag = "10" then do shell script "/System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine -background > /dev/null 2>&1 &" else if aTag = "20" then killScreenSaver() of me set curMode to missing value end if end if end actionHandler: on killScreenSaver() set shellText to "killall ScreenSaverEngine" do shell script shellText end killScreenSaver |
soundIO Libで現在のサウンド入出力デバイス名を取得
現在設定されているサウンド入出力デバイス名を取得するAppleScriptです。
AppleScript名:soundIO Libで現在のサウンド入出力デバイス名を取得 |
use AppleScript version "2.4" use scripting additions use soundIO : script "soundIO Lib" version "1.2" without importing set i2 to soundIO’s getCurrentAudioInuptDevice() set o2 to soundIO’s getCurrentAudioOutuptDevice() return {i2, o2} –> {"Built-in Microphone", "Built-in Output"} |
soundIO Libでサウンド入出力を変更 v2.0
サウンドの入出力デバイスを任意のデバイスに変更するAppleScriptです。
–> soundIO Lib (To ~/Library/Script Libraries)
AppleScript名:soundIO Libでサウンド入出力を変更 v2.0 |
— Created 2016-10-07 by Takaaki Naganoya — 2016 Piyomaru Software use AppleScript version "2.4" use scripting additions use soundIO : script "soundIO Lib" version "1.2" without importing set outList to soundIO’s getEveryAudioOutputDevice() set targOutputDevice to contents of first item of (choose from list outList with prompt "Select Sound Output Device") set inList to soundIO’s getEveryAudioInputDevice() set targIntputDevice to contents of first item of (choose from list inList with prompt "Select Sound Intput Device") –入出力デバイスを設定 set i1 to soundIO’s setAudioInuptDevice(targIntputDevice) set o1 to soundIO’s setAudioOutuptDevice(targOutputDevice) set aRes to ({i1, o1} = {true, true}) return aRes |
soundIO Libでサウンド入出力を標準デバイスに設定
サウンドの入出力デバイスを標準デバイスに変更するAppleScriptです。
ただし、Mac miniではデフォルトのオーディオ入力デバイスが存在していないため、エラーになります。
AppleScript名:soundIO Libでサウンド入出力を標準デバイスに設定 |
use AppleScript version "2.4" use scripting additions use soundIO : script "soundIO Lib" version "1.2" without importing set targOutputDevice to "Built-in Output" set targIntputDevice to "Built-in Microphone" –Mac mini does not have default sound input device –出力デバイス一覧に設定対象が入っているかチェック set aList to soundIO’s getEveryAudioOutputDevice() if targOutputDevice is not in aList then return {false, "Target output device seems to not present"} –入力デバイス一覧に設定対象が入っているかチェック set bList to soundIO’s getEveryAudioInputDevice() if targIntputDevice is not in bList then return {false, "Target input device seems to not present"} –入出力デバイスを設定 set i1 to soundIO’s setAudioInuptDevice(targIntputDevice) set o1 to soundIO’s setAudioOutuptDevice(targOutputDevice) –サウンド入出力デバイスの変更確認 set i2 to soundIO’s getCurrentAudioInuptDevice() set o2 to soundIO’s getCurrentAudioOutuptDevice() set aRes to ({i1, o1} = {true, true}) and ({i2, o2} = {targIntputDevice, targOutputDevice}) return aRes |
soundIO Libでサウンド入出力をSoundFlowerに設定
サウンドの入出力デバイスをSoundFolwerに変更するAppleScriptです。
SoundFlowerをインストールしていない環境ではエラーになります。
AppleScript名:soundIO Libでサウンド入出力をSoundFlowerに設定 |
use AppleScript version "2.4" use scripting additions use soundIO : script "soundIO Lib" version "1.2" without importing set targDevice to "Soundflower (2ch)" set aList to soundIO’s getEveryAudioOutputDevice() if targDevice is not in aList then return false set bList to soundIO’s getEveryAudioInputDevice() if targDevice is not in bList then return false set i1 to soundIO’s setAudioInuptDevice(targDevice) set o1 to soundIO’s setAudioOutuptDevice(targDevice) set i2 to soundIO’s getCurrentAudioInuptDevice() set o2 to soundIO’s getCurrentAudioOutuptDevice() set aRes to (i1 = true and o1 = true) and (i2 = targDevice and o2 = targDevice) return aRes |
soundIO Libでサウンド入出力デバイス名一覧を取得
サウンドの入出力デバイス名の一覧を取得するAppleScriptです。
サウンドの入出力先を取得したり変更するのにシステム環境設定をGUI Scripting経由で操作している例をよく見かけますが、あまり上品なやり方ではないのでこのようなライブラリを利用することをお勧めします。
use soundIO : script “soundIO Lib” version “1.2” without importing
と、useコマンドでAppleScript Librariesを読み込む際に、オプションで「without importing」を指定しています。
これは、デフォルトの状態ではライブラリ本体の書き換えがすぐに反映されなかった(キャッシュされていた)ことに対処したものです。それほど頻繁にライブラリ側の書き換えを行わなければ、指定する必要はないでしょう。
AppleScript名:soundIO Libでサウンド入出力デバイス名一覧を取得 |
use AppleScript version "2.4" use scripting additions use soundIO : script "soundIO Lib" version "1.2" without importing set outList to soundIO’s getEveryAudioOutputDevice() –> {"Built-in Output", "Mobiola Headphone", "Mobiola Microphone", "Soundflower (2ch)", "Soundflower (64ch)"} set inList to soundIO’s getEveryAudioInputDevice() –> {"Built-in Microphone", "Mobiola Headphone", "Mobiola Microphone", "Soundflower (2ch)", "Soundflower (64ch)"} |