AppleScript名:バッテリの容量を取得する |
–バッテリ容量 set cap to (do shell script "ioreg -l | grep MaxCapacity | sed ’s/^.*MaxCapacity\" = \\([0-9]*\\)$/\\1/g’") as {number, anything} |
カテゴリー: System
ACで動いているかDCで動いているかを取得する
AppleScript名:ACで動いているかDCで動いているかを取得する |
set curPowerStat to retACDC() of me
–ACで動作中かDCで動作中かを返す on retACDC() return word -2 of paragraph 1 of (do shell script "pmset -g cap") end retACDC |
ドライブマウント検出
ドライブのマウントを検出してsayコマンドで通知するAppleScriptです。
AppleScript名:ドライブマウント検出 |
— Created 2016-10-27 by Takaaki Naganoya — 2016 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" on run current application’s NSWorkspace’s sharedWorkspace()’s notificationCenter()’s addObserver:me selector:"onVolumeMount:" |name|:"NSWorkspaceDidMountNotification" object:(missing value) current application’s NSWorkspace’s sharedWorkspace()’s notificationCenter()’s addObserver:me selector:"onVolumeUnmount:" |name|:"NSWorkspaceDidUnmountNotification" object:(missing value) end run on onVolumeMount:aNotif say "マウントしました" using "Kyoko" –Mount Message In Japanese end onVolumeMount: on onVolumeUnmount:aNotif say "アンマウントしました" using "Kyoko" –Unmount Message In Japanese end onVolumeUnmount: |
TouchBarの検出
AppleScript名:TouchBarの検出 |
set aRes to detectTouchBar() of me –> true/false on detectTouchBar() tell application "System Events" set frontApp to first application process whose frontmost is true try set touchBar to first UI element of frontApp whose role is "AXFunctionRowTopLevelElement" on error errMsg number errNum return false end try set touchBarItems to value of attribute "AXChildren" of touchBar return (touchBarItems is not equal to {}) end tell end detectTouchBar |
USBに接続されたUSBメモリの情報を取得する v2
AppleScript名:USBに接続されたUSBメモリの情報を取得する v2 |
set tmpPath to POSIX path of (path to temporary items from system domain) set aFileName to (do shell script "/usr/bin/uuidgen") set outPath to tmpPath & aFileName & ".plist" do shell script "/usr/sbin/system_profiler -xml SPUSBDataType > " & outPath tell application "System Events" set vRec to value of property list file (outPath as string) set v1Rec to _items of (first item of vRec) set dList to {} set sList to {} repeat with i in v1Rec set hitF to false try set j to _items of i set hitF to true end try if hitF = true then repeat with jj in j try set jjj to volumes of jj set sNum to d_serial_num of jj set vStr to b_vendor_id of jj repeat with ii in jjj set the end of dList to {serialNum:sNum, venderName:vStr, dData:contents of ii} end repeat end try end repeat end if end repeat end tell dList –> {{serialNum:"7f12db856195ef", venderName:"0x056e (Elecom Co., Ltd.)", dData:{mount_point:"/Volumes/NO NAME", _name:"NO NAME", writable:"yes", bsd_name:"disk2s1", free_space:"3.75 GB", file_system:"MS-DOS FAT32", |size|:"3.77 GB"}}, {serialNum:"M004101800001", venderName:"0x4146", dData:{mount_point:"/Volumes/ぴよまる", _name:"ぴよまる", writable:"yes", bsd_name:"disk1s9", free_space:"56 MB", file_system:"HFS+", |size|:"123.1 MB"}}} |
使用中のMacの製品呼称を取得する v3
AppleScript名:使用中のMacの製品呼称を取得する v3 |
use AppleScript version "2.5" use scripting additions use framework "Foundation" set mRes to retModelInfo() of me –> "MacBook Pro with Retina display, Intel Core i7, 15\" (Mid 2012)" on retModelInfo() set pListPath to "/System/Library/PrivateFrameworks/ServerInformation.framework/" & "Versions/A/Resources/English.lproj/SIMachineAttributes.plist" set aRec to retDictFromPlist(pListPath) of me set hwName to (do shell script "sysctl -n hw.model") –> "MacBookPro10,1" set aMachineRec to retRecordByLabel(aRec, hwName) set aMachineRec2 to contents of first item of aMachineRec return (marketingModel of _LOCALIZABLE_ of aMachineRec2) end retModelInfo –Read plist as record on retDictFromPlist(aPath) set thePath to current application’s NSString’s stringWithString:aPath set thePath to thePath’s stringByExpandingTildeInPath() set theDict to current application’s NSDictionary’s dictionaryWithContentsOfFile:thePath return theDict as record end retDictFromPlist –リストに入れたレコードを、指定の属性ラベルの値で抽出 on filterRecListByLabel(aRecList as list, aPredicate as string) –ListからNSArrayへの型変換 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 –NSArrayからListに型変換して返す return filteredArray as list end filterRecListByLabel –指定レコードの指定ラベルの値を取得する on retRecordByLabel(aRec as record, aKey as string) set aDic to current application’s NSDictionary’s dictionaryWithDictionary:aRec set aVal to aDic’s valueForKey:aKey return aVal as list end retRecordByLabel –指定レコードの指定ラベルの値を取得する on retRecordByKeyPath(aRec as record, aKey as string) set aDic to current application’s NSDictionary’s dictionaryWithDictionary:aRec set aVal to aDic’s valueForKeyPath:aKey return aVal end retRecordByKeyPath |
CPUのBrand Nameとバス速度を表示
AppleScript名:CPUのBrand Nameとバス速度を表示 |
set aTargLabel to "machdep.cpu.brand_string:" set aRes to retFilteredSysctrlValue(aTargLabel) of me –> "Intel(R) Core(TM) i7-3720QM CPU @ 2.60GHz" set bRes to retFilteredSysctrlValue("hw.busfrequency_max") of me –> " 100000000" return {aRes, bRes} –> {"Intel(R) Core(TM) i7-3720QM CPU @ 2.60GHz", " 100000000"} on retFilteredSysctrlValue(aTargLabel) set aRes to do shell script "sysctl -a | grep " & aTargLabel set aLen to length of aTargLabel set bRes to text (aLen + 2) thru -1 of aRes return bRes end retFilteredSysctrlValue |
使用CPUの状況を取得
AppleScript名:使用CPUの状況を取得 |
— Created 2017-12-17 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set aDict to retCpuUsage() of me –> {userCPU:3.2, sysCPU:12.29, idleCPU:84.49} on retCpuUsage() set cpuUseRes to do shell script "top -l 1 | head -10 | grep ’CPU usage’" set aRes to (parseStrFromTo(cpuUseRes, " ", "%") of me) set userUsage to (last item of (words of item 1 of aRes)) as real set sysUsage to (item 2 of aRes) as real set idleUsage to (item -2 of aRes) as real return {userCPU:userUsage, sysCPU:sysUsage, idleCPU:idleUsage} end retCpuUsage on parseStrFromTo(aParamStr, fromStr, toStr) set theScanner to current application’s NSScanner’s scannerWithString:aParamStr set anArray to current application’s NSMutableArray’s array() repeat until (theScanner’s isAtEnd as boolean) — terminate check, return the result (aDict) to caller set {theResult, theKey} to theScanner’s scanUpToString:fromStr intoString:(reference) — skip over separator theScanner’s scanString:fromStr intoString:(missing value) set {theResult, theValue} to theScanner’s scanUpToString:toStr intoString:(reference) if theValue is missing value then set theValue to "" –>追加 — skip over separator theScanner’s scanString:toStr intoString:(missing value) anArray’s addObject:theValue end repeat return anArray as list end parseStrFromTo |
PFSystemKitでMac本体の情報を取得 v1.0
AppleScript名:PFSystemKitでMac本体の情報を取得 v1.0 【Comment】 –https://github.com/perfaram/PFSystemKit |
— Created 2016-07-26 by Takaaki Naganoya — 2016 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "PFSystemKit" set systemKit to current application’s PFSystemKit’s investigate() set a1 to systemKit’s platformReport()’s serial() as string –Hardware Serial –> "C0XXXXXXXXX2" set a2 to systemKit’s platformReport()’s model() as string –Hardware Model –> "MacBookPro10,1�" set a3 to systemKit’s platformReport()’s family() as integer –Hardware Family –> 5 set a4 to systemKit’s platformReport()’s uuid() as string –Hardware UUID –> "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" set a5 to systemKit’s platformReport()’s memorySize() as integer –RAM Size –> 8 set a6 to systemKit’s platformReport()’s boardID() as string –> "Mac-XXXXXXXXXXXXXXXX" set a7 to systemKit’s platformReport()’s romVersion() as string –> "MBP101.XXX.XXXX.XXX.XXXXXXXXXX" set a8 to systemKit’s platformReport()’s romReleaseDate() as date –> date 4017年8月8日火曜日 0:00:00 set a9 to systemKit’s platformReport()’s smcVersion() as string –SMC Version –> "2.3f36" set a10 to systemKit’s platformReport()’s sleepCause() as string –> "5" set a11 to systemKit’s platformReport()’s shutdownCause() as string –> "5" set a12 to systemKit’s platformReport()’s platform() as string –> "1" set a13 to systemKit’s platformReport()’s endianness() as string –> "0" |
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)"} |