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

カテゴリー: System

ACで動いているかDCで動いているかを取得する

Posted on 2月 20, 2018 by Takaaki Naganoya
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

★Click Here to Open This Script 

Posted in System | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

ドライブマウント検出

Posted on 2月 20, 2018 by Takaaki Naganoya

ドライブのマウントを検出して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:

★Click Here to Open This Script 

Posted in Noification System | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

TouchBarの検出

Posted on 2月 20, 2018 by Takaaki Naganoya
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

★Click Here to Open This Script 

Posted in System | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

USBに接続されたUSBメモリの情報を取得する v2

Posted on 2月 20, 2018 by Takaaki Naganoya
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"}}}

★Click Here to Open This Script 

Posted in System XML | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

使用中のMacの製品呼称を取得する v3

Posted on 2月 20, 2018 by Takaaki Naganoya
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

★Click Here to Open This Script 

Posted in System | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

CPUのBrand Nameとバス速度を表示

Posted on 2月 20, 2018 by Takaaki Naganoya
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

★Click Here to Open This Script 

Posted in System | Tagged 10.11savvy 10.12savvy 10.13savvy | 1 Comment

使用CPUの状況を取得

Posted on 2月 20, 2018 by Takaaki Naganoya
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

★Click Here to Open This Script 

Posted in System | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

PFSystemKitでMac本体の情報を取得 v1.0

Posted on 2月 20, 2018 by Takaaki Naganoya

–> PFSystemKit.framework

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"

★Click Here to Open This Script 

Posted in System | Leave a comment

Bluetoothに接続中のデバイス名を取得するv4

Posted on 2月 18, 2018 by Takaaki Naganoya
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

★Click Here to Open This Script 

Posted in Bluetooth System | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

Bluetoothのオン、オフ状態を取得する

Posted on 2月 18, 2018 by Takaaki Naganoya
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

★Click Here to Open This Script 

Posted in Bluetooth System | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

Bluetoothに接続中のデバイス名を取得、AirPodsを切断する

Posted on 2月 18, 2018 by Takaaki Naganoya
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

★Click Here to Open This Script 

Posted in Bluetooth System | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

Bluetoothに接続中のデバイスをメーカー名とマイナー種別で抽出

Posted on 2月 18, 2018 by Takaaki Naganoya
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

★Click Here to Open This Script 

Posted in Bluetooth System | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

NSProcessInfoでプロセスの各種情報を取得

Posted on 2月 18, 2018 by Takaaki Naganoya
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

★Click Here to Open This Script 

Posted in System | Tagged 10.11savvy 10.12savvy 10.13savvy | 2 Comments

デスクトップ・スクリーンセーバー

Posted on 2月 17, 2018 by Takaaki Naganoya

ステータスバーアイテムを作成してメニューバー上にメニューを出し、デスクトップ・スクリーンセーバーのコントロールを行うAppleScriptです。

ステータスバーアイテムを使って、簡単なメニュー操作を行うAppleScriptの試作品です。メニュー操作の方がメインで、デスクトップ・スクリーンセーバーの方はオマケです。

–> DEMO Movie

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

★Click Here to Open This Script 

Posted in GUI Screen Saver System | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

soundIO Libで現在のサウンド入出力デバイス名を取得

Posted on 2月 17, 2018 by Takaaki Naganoya

現在設定されているサウンド入出力デバイス名を取得するAppleScriptです。

–> soundIO Lib

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"}

★Click Here to Open This Script 

Posted in Sound System | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

soundIO Libでサウンド入出力を変更 v2.0

Posted on 2月 17, 2018 by Takaaki Naganoya

サウンドの入出力デバイスを任意のデバイスに変更する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

★Click Here to Open This Script 

Posted in Sound System | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

soundIO Libでサウンド入出力を標準デバイスに設定

Posted on 2月 17, 2018 by Takaaki Naganoya

サウンドの入出力デバイスを標準デバイスに変更するAppleScriptです。

ただし、Mac miniではデフォルトのオーディオ入力デバイスが存在していないため、エラーになります。

–> soundIO Lib

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

★Click Here to Open This Script 

Posted in Sound System | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

soundIO Libでサウンド入出力をSoundFlowerに設定

Posted on 2月 17, 2018 by Takaaki Naganoya

サウンドの入出力デバイスをSoundFolwerに変更するAppleScriptです。

SoundFlowerをインストールしていない環境ではエラーになります。

–> soundIO Lib

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

★Click Here to Open This Script 

Posted in Sound System | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

soundIO Libでサウンド入出力デバイス名一覧を取得

Posted on 2月 17, 2018 by Takaaki Naganoya

サウンドの入出力デバイス名の一覧を取得するAppleScriptです。

サウンドの入出力先を取得したり変更するのにシステム環境設定をGUI Scripting経由で操作している例をよく見かけますが、あまり上品なやり方ではないのでこのようなライブラリを利用することをお勧めします。

–> soundIO Lib

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)"}

★Click Here to Open This Script 

Posted in Sound System | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

指定フォルダ以下のすべてのファイルのうち指定UTIに該当するものを取得(spotlightで処理)

Posted on 2月 15, 2018 by Takaaki Naganoya
AppleScript名:指定フォルダ以下のすべてのファイルを再帰で取得(spotlightで処理).scpt
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
use mdLib : script "Metadata Lib" version "1.0.0" –https://www.macosxautomation.com/applescript/apps/Script_Libs.html#Metadata_Lib

–set theFolder to choose folder
set theFolder to (path to desktop folder)
set theFiles to mdLib’s searchFolders:{theFolder} searchString:"kMDItemContentType IN[c] %@" searchArgs:{"com.apple.applescript.script", "com.apple.applescript.script-bundle"}

★Click Here to Open This Script 

Posted in file Spotlight System | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

Post navigation

  • Older posts
  • Newer posts

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

Google Search

Popular posts

  • 開発機としてM2 Mac miniが来たのでガチレビュー
  • macOS 15, Sequoia
  • Pages本執筆中に、2つの書類モード切り替えに気がついた
  • Numbersで選択範囲のセルの前後の空白を削除
  • メキシカンハットの描画
  • Pixelmator Pro v3.6.4でAppleScriptからの操作時の挙動に違和感が
  • AppleScriptによる並列処理
  • Safariで「プロファイル」機能を使うとAppleScriptの処理に影響
  • macOS 15でも変化したText to Speech環境
  • AppleScript入門③AppleScriptを使った「自動化」とは?
  • デフォルトインストールされたフォント名を取得するAppleScript
  • macOS 15 リモートApple Eventsにバグ?
  • 【続報】macOS 15.5で特定ファイル名パターンのfileをaliasにcastすると100%クラッシュするバグ
  • AppleScript入門① AppleScriptってなんだろう?
  • Script Debuggerの開発と販売が2025年に終了
  • macOS 14で変更になったOSバージョン取得APIの返り値
  • NSObjectのクラス名を取得 v2.1
  • 有害ではなくなっていたSpaces
  • macOS 15:スクリプトエディタのAppleScript用語辞書を確認できない
  • Xcode上のAppleScriptObjCのプログラムから、Xcodeのログ欄へのメッセージ出力を実行

Tags

10.11savvy (1101) 10.12savvy (1242) 10.13savvy (1391) 10.14savvy (587) 10.15savvy (438) 11.0savvy (283) 12.0savvy (212) 13.0savvy (194) 14.0savvy (147) 15.0savvy (135) CotEditor (66) Finder (51) iTunes (19) Keynote (119) NSAlert (61) NSArray (51) NSBitmapImageRep (20) NSBundle (20) NSButton (34) NSColor (53) NSDictionary (28) NSFileManager (23) NSFont (21) NSImage (41) NSJSONSerialization (21) NSMutableArray (63) NSMutableDictionary (22) NSPredicate (36) NSRunningApplication (56) NSScreen (30) NSScrollView (22) NSString (119) NSURL (98) NSURLRequest (23) NSUTF8StringEncoding (30) NSView (33) NSWorkspace (20) Numbers (76) Pages (55) Safari (44) Script Editor (27) WKUserContentController (21) WKUserScript (20) WKWebView (23) WKWebViewConfiguration (22)

カテゴリー

  • 2D Bin Packing
  • 3D
  • AirDrop
  • AirPlay
  • Animation
  • AppleScript Application on Xcode
  • Beginner
  • Benchmark
  • beta
  • Bluetooth
  • Books
  • boolean
  • bounds
  • Bug
  • Calendar
  • call by reference
  • check sum
  • Clipboard
  • Cocoa-AppleScript Applet
  • Code Sign
  • Color
  • Custom Class
  • date
  • dialog
  • diff
  • drive
  • Droplet
  • 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
  • Localize
  • Machine Learning
  • Map
  • Markdown
  • Menu
  • Metadata
  • MIDI
  • MIME
  • Natural Language Processing
  • Network
  • news
  • Noification
  • Notarization
  • Number
  • Object control
  • OCR
  • OSA
  • parallel processing
  • PDF
  • Peripheral
  • process
  • 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)
  • 未分類

アーカイブ

  • 2025年6月
  • 2025年5月
  • 2025年4月
  • 2025年3月
  • 2025年2月
  • 2025年1月
  • 2024年12月
  • 2024年11月
  • 2024年10月
  • 2024年9月
  • 2024年8月
  • 2024年7月
  • 2024年6月
  • 2024年5月
  • 2024年4月
  • 2024年3月
  • 2024年2月
  • 2024年1月
  • 2023年12月
  • 2023年11月
  • 2023年10月
  • 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