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

月: 2018年2月

数値文字列に対して、少数点以下の数値の切り上げ、切り下げ v2

Posted on 2月 21, 2018 by Takaaki Naganoya
AppleScript名:数値文字列に対して、少数点以下の数値の切り上げ、切り下げ v2
— Created 2017-08-12 by Takaaki Naganoya
— Modified 2017-08-28 by Shane Stanley
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

property NSString : a reference to current application’s NSString
property NSNumber : a reference to current application’s NSNumber
property NSNumberFormatter : a reference to current application’s NSNumberFormatter
property NSNumberFormatterRoundUp : a reference to current application’s NSNumberFormatterRoundUp
property NSNumberFormatterRoundDown : a reference to current application’s NSNumberFormatterRoundDown

set a to "0.9096617698669434"
set a1Res to roundingDownNumStr(a, 2) of me

set b to "0.0001830748806241899"
set b1Res to roundingDownNumStr(b, 2) of me

return {a1Res, b1Res}
–>  {​​​​​0.9, ​​​​​0.0​​​}

on roundingDownNumStr(aNum as string, aDigit as integer)
  set a to NSString’s stringWithString:aNum
  
set aa to a’s doubleValue()
  
set aFormatter to NSNumberFormatter’s alloc()’s init()
  
aFormatter’s setMaximumFractionDigits:aDigit
  
aFormatter’s setRoundingMode:(NSNumberFormatterRoundDown)
  
set aStr to aFormatter’s stringFromNumber:aa
  
return (aStr as text) as real
end roundingDownNumStr

on roundingUpNumStr(aNum as string, aDigit as integer)
  set a to NSString’s stringWithString:aNum
  
set aa to a’s doubleValue()
  
set aFormatter to NSNumberFormatter’s alloc()’s init()
  
aFormatter’s setMaximumFractionDigits:aDigit
  
aFormatter’s setRoundingMode:(NSNumberFormatterRoundUp)
  
set aStr to aFormatter’s stringFromNumber:aa
  
return (aStr as text) as real
end roundingUpNumStr

★Click Here to Open This Script 

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

数値書式関連

Posted on 2月 21, 2018 by Takaaki Naganoya
AppleScript名:数値書式関連
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

set theNumber to 1.234567890123E+12
set theResult to formatNumber_(theNumber)
–> "1,234,567,890,123"

set aRes to textFromNumber_forLocale_(1.2345678E+4, "fr")
–> "douze-mille-trois-cent-quarante-cinq virgule six sept huit"

set aRes to textFromNumber_forLocale_(1.2345678E+4, "ja")
–> "一万二千三百四十五・六七八"

set aRes to formatNumber_usingFormat_(1.2345678E+4, "#,###.00;0.00;(#,##0.00)")
–> "12,345.68"

on formatNumber:theNumber
  set theFormatter to current application’s NSNumberFormatter’s new()
  
theFormatter’s setNumberStyle:(current application’s NSNumberFormatterDecimalStyle)
  
set theResult to theFormatter’s stringFromNumber:theNumber
  
return theResult as text
end formatNumber:

on textFromNumber:theNumber forLocale:localeString
  set theFormatter to current application’s NSNumberFormatter’s new()
  
theFormatter’s setNumberStyle:(current application’s NSNumberFormatterSpellOutStyle)
  
set theLocale to current application’s NSLocale’s localeWithLocaleIdentifier:localeString
  
theFormatter’s setLocale:theLocale
  
set theResult to theFormatter’s stringFromNumber:theNumber
  
return theResult as text
end textFromNumber:forLocale:

on formatNumber:theNumber usingFormat:formatString
  set theFormatter to current application’s NSNumberFormatter’s new()
  
theFormatter’s setFormat:formatString
  
theFormatter’s setLocalizesFormat:false
  
set theResult to theFormatter’s stringFromNumber:theNumber
  
return theResult as text
end formatNumber:usingFormat:

★Click Here to Open This Script 

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

大きな数値同士を足し算する

Posted on 2月 21, 2018 by Takaaki Naganoya

巨大な数値同士を加算するAppleScriptです。

AppleScriptの数値型データが±99999999程度で指数表示になってしまうため、巨大な数値を扱うのには向いていないのですが、直接数値で扱わずに文字列として保持すれば問題はありません。

数値文字同士の計算についても、bcコマンドを使うことで四則演算+α程度は行えるので、あまり問題はないでしょう。bcコマンドを呼び出すオーバーヘッドはそれなりにあるので、すべての数値を文字列で保持したりするのは得策ではありません。必要な箇所だけ使う、といったところです。

AppleScript名:大きな数値同士を足し算する
set a to "100000000000000000"
set b to "200000000000000000"

set c to addLargeNumber(a, b) of me

on addLargeNumber(aNum, bNum)
  set aNumStr to aNum as string
  
set bNumStr to bNum as string
  
  
set aCMD to "echo \" scale=10; " & aNumStr & "+" & bNumStr & " \" | bc"
  
set aRes to do shell script aCMD
  
return aRes
end addLargeNumber

★Click Here to Open This Script 

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

与えられた数が素数かどうかチェック(PrimeNumberHelper)

Posted on 2月 21, 2018 by Takaaki Naganoya

与えられた数値が素数かどうかチェックを行うAppleScriptです。

素数チェックには、外部フレームワーク「primeNumKit.framework」を呼び出しています。

素数チェックや素因数分解は意外と実務でも必要になる機会が多く、高速な計算ルーチンが使えると便利です。

–> primeNumKit.framework

AppleScript名:与えられた数が素数かどうかチェック(PrimeNumberHelper)
— Created 2017-05-18 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "primeNumKit" –https://github.com/NazarYavornytskyy/prime-number-objc

set aPrime to current application’s PrimeNumberHelper’s alloc()’s init()
set a to 75777773
set aRes to (aPrime’s isPrime:a) as boolean

★Click Here to Open This Script 

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

簡単な素因数分解v5

Posted on 2月 21, 2018 by Takaaki Naganoya

与えられた数値の素因数分解を行うAppleScriptです。

素数チェックには、数値が小さい場合にはOLD Style AppleScriptの計算ルーチンを呼び出し、数値が大きい(=計算に時間がかかる)場合には外部フレームワーク「primeNumKit.framework」を呼び出すようにしています。

AppleScriptからのCocoaの機能呼び出しにも時間がかかるので、細かい処理を頻繁に呼び出すと意外とAppleScriptだけで処理したほうが速かったりします(素数チェックを行うためにループで割り算を行いますが、最大値は2から目的値の1/2まででよく、こうした地道な改善が予想外に効いています)。

そのため、計算量を見てどちらで処理するかを判定しています。

–> primeNumKit.framework

AppleScript名:簡単な素因数分解v5
— Created 2017-05-18 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "primeNumKit" –https://github.com/NazarYavornytskyy/prime-number-objc

set aRes to getPrimeFactor(56) of me
–> {2, 2, 2, 7}

set aRes to getPrimeFactor(54) of me
–> {2, 3, 3, 3}

set aRes to getPrimeFactor(9999991) of me
–> {9999991} –素数

–与えられた数を素因数分解する
on getPrimeFactor(aTargNum)
  copy aTargNum to a
  
set pFactList to {}
  
  
repeat
    set pRes to checkPrimeNumber(a) of me
    
if pRes = true then
      set the end of pFactList to a
      
return pFactList
    end if
    
    
set aFactor to checkFactor(a) of me
    
set the end of pFactList to aFactor
    
set a to a div aFactor
  end repeat
end getPrimeFactor

–素数チェック
on checkPrimeNumber(aNum)
  if aNum < 10000 then –0.25秒以上かかりそうな処理はCocoaで処理(実測値ベース)
    return checkPrimeNumberSub1(aNum) of me
  else
    set aPrime to current application’s PrimeNumberHelper’s alloc()’s init()
    
return (aPrime’s isPrime:aNum) as boolean
  end if
end checkPrimeNumber

–小さい数の場合の素数検出
on checkPrimeNumberSub1(aNum)
  repeat with i from 2 to (aNum div 2)
    if aNum mod i is equal to 0 then
      return i –false
    end if
  end repeat
  
return true
end checkPrimeNumberSub1

–素因数チェック
on checkFactor(aNum)
  repeat with i from 2 to (aNum div 2)
    if aNum mod i is equal to 0 then
      return i
    end if
  end repeat
end checkFactor

★Click Here to Open This Script 

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

指定のコード体系の全パターンのコードを生成 v5

Posted on 2月 21, 2018 by Takaaki Naganoya

AppleScript名:指定のコード体系の全パターンのコードを生成 v5
–v5 外部からプロパティで与えられたルールから、初期値となる「最小値」を自前で計算するように変更
–v4 桁ごとにサブルーチンを設けるのではなく、再帰処理で1つのルーチンを多重呼び出しするように変更
–v3 コードのルールを外部供給する構成にした(処理ロジックとルールの分離が完了)
–v2 各桁の最大値と最小値をプロパティで持たせるテスト
–v1 各桁のインクリメント用のサブルーチンを作成し、ルールを各サブルーチン側でハードコーディングする(正しく動く)

script spd
  property aList : {}
  
property aRuleList : {{1, 2}, {1, 3}, {0, 1}, {1, 4}, {1, 8}} –各桁の{最小値, 最大値}ペアのリスト
  
property aRuleLen : length of aRuleList
end script

set aList of spd to {} –initilaize

set initNum to getMinNum() of me –本ルール下における最小値

set the end of aList of spd to initNum

copy initNum to aNum

repeat
  set aRes to incDigit(aNum, 1) of me
  
  
if aRes = false then
    exit repeat
  end if
  
  
set the end of aList of spd to aRes
  
  
copy aRes to aNum
  
end repeat

–return length of (aList of spd)
return (aList of spd)

–与えられたルール下における最小値をルールリストから求める
on getMinNum()
  –桁数が合っているだけのダミー数字を、適切な桁数作成する(例:11111)
  
set tmpNumStr to ""
  
repeat (aRuleLen of spd) times
    set tmpNumStr to tmpNumStr & "1"
  end repeat
  
  
set tmpNum to tmpNumStr as integer
  
  
–ルールから各桁の最小値を取り出して、各桁に設定する
  
repeat with i from 1 to (aRuleLen of spd)
    set aDigNum to item 1 of item i of (aRuleList of spd)
    
set tmpNum to setDigit(tmpNum, i, aDigNum) of me
  end repeat
  
  
return tmpNum
  
end getMinNum

–繰り上がり処理(再帰呼び出しで使用)
on incDigit(aNum, aDigit)
  
  
set {thisMin, thisMax} to item ((aRuleLen of spd) – aDigit + 1) of (aRuleList of spd)
  
  
set aTarget to getDigit(aNum, aDigit) of me
  
  
if aTarget = thisMax then
    
    
if aDigit = (aRuleLen of spd) then
      –オーバーフロー(桁あふれ)エラーを返す
      
return false
    end if
    
    
set bNum to incDigit(aNum, aDigit + 1) of me
    
    
if bNum = false then return false
    
    
set bNum to setDigit(bNum, aDigit, thisMin) of me
    
  else
    
    
set aTarget to aTarget + 1
    
set bNum to setDigit(aNum, aDigit, aTarget) of me
    
  end if
  
  
return bNum
  
end incDigit

–指定数値のうち指定桁の数字を返す
on getDigit(aNum, aDigit)
  
  
set aStr to aNum as string
  
set aLen to length of aStr
  
if aLen < aDigit then
    return false –エラー
  end if
  
  
set tStr to character (aLen – aDigit + 1) of aStr
  
return tStr as integer
  
end getDigit

–指定数値のうち指定桁の数字を返す
on setDigit(aNum, aDigit, newNum)
  
  
set aStr to aNum as string
  
set aLen to length of aStr
  
if aLen < aDigit then
    return false –エラー
  end if
  
  
set aList to characters of aStr
  
  
set item (aLen – aDigit + 1) of aList to (newNum as string)
  
set aaStr to aList as string
  
  
return aaStr as integer
  
end setDigit

★Click Here to Open This Script 

Posted in recursive call Text | Tagged 10.11savvy 10.12savvy 10.13savvy | 1 Comment

Macの製品カテゴリ名を取得する

Posted on 2月 20, 2018 by Takaaki Naganoya
AppleScript名:Macの製品カテゴリ名を取得する
— Created 2015-11-01 by Takaaki Naganoya
— 2015 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set macRes to getSystemProfileInAGenre("SPHardwareDataType", "machine_name") of me
–>  "MacBook Pro"

on getSystemProfileInAGenre(aTargGenre as string, aTargKey as string)
  set sRes to do shell script ("/usr/sbin/system_profiler -xml " & aTargGenre)
  
set aSource to (readPlistFromStr(sRes) of me) as list
  
set aaList to contents of first item of aSource
  
  
set aList to _items of aaList
  
repeat with i in aList
    set aDict to (current application’s NSMutableDictionary’s dictionaryWithDictionary:(contents of i))
    
set aKeyList to (aDict’s allKeys()) as list
    
if aTargKey is in aKeyList then
      set aRes to (aDict’s valueForKeyPath:aTargKey)
      
if aRes is not equal to missing value then
        return aRes as string
      end if
    end if
  end repeat
  
  
return false
end getSystemProfileInAGenre

–stringのplistを読み込んでRecordに
on readPlistFromStr(theString)
  set aSource to current application’s NSString’s stringWithString:theString
  
set pListData to aSource’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
  
set aPlist to current application’s NSPropertyListSerialization’s propertyListFromData:pListData mutabilityOption:(current application’s NSPropertyListImmutable) |format|:(current application’s NSPropertyListFormat) errorDescription:(missing value)
  
return aPlist
end readPlistFromStr

★Click Here to Open This Script 

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

ソフトウェア的にアンマウントしたが物理的に接続解除していないHard Driveの名前を取得する v2

Posted on 2月 20, 2018 by Takaaki Naganoya

ソフトウェア的にアンマウントした状態で、各種接続端子(USBとか)で物理的につながったままの外部ドライブの名称を取得するAppleScriptです。

いちど、そういう質問が来て「できるわけがない」という回答をしていました。AppleScriptの性質上、ソフトウェア的にそういう操作は許可されていません。そもそも、そんな機能はありません。

ただ、まさかshell command経由でそうした操作ができるとは知りませんでした。冗談半分で調べてみたら存在したという状況です。

AppleScript名:ソフトウェア的にアンマウントしたが物理的に接続解除していないHard Driveの名前を取得する v2
— Created 2017-06-20 Shane Stanley
— Modified 2017-06-20 Takaaki Naganoya
use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set theResult to do shell script "diskutil list -plist"

— make dictionary from property list
set theResult to current application’s NSString’s stringWithString:theResult
set pListDict to theResult’s propertyList()
–>  (NSDictionary) {​​​​​AllDisks:{​​​​​​​"disk0", ​​​​​​​"disk0s1", ​​​​​​​"disk0s2", ​​​​​​​"disk0s3", ​​​​​​​"disk1"​​​​​}, ​​​​​AllDisksAndPartitions:{​​​​​​​{​​​​​​​​​DeviceIdentifier:"disk0", ​​​​​​​​​Size:500277790720, ​​​​​​​​​Content:"GUID_partition_scheme", ​​​​​​​​​Partitions:{​​​​​​​​​​​{​​​​​​​​​​​​​DiskUUID:"FC89778D-9028-4FAA-8118-F74802B0664E", ​​​​​​​​​​​​​DeviceIdentifier:"disk0s1", ​​​​​​​​​​​​​Size:209715200, ​​​​​​​​​​​​​Content:"EFI", ​​​​​​​​​​​​​VolumeName:"EFI", ​​​​​​​​​​​​​VolumeUUID:"85D67001-D93E-3687-A1C2-79D677F0C2E0"​​​​​​​​​​​}, ​​​​​​​​​​​{​​​​​​​​​​​​​DiskUUID:"AAF54C8C-9229-4DC1-8EA5-5FCCE9050DC4", ​​​​​​​​​​​​​DeviceIdentifier:"disk0s2", ​​​​​​​​​​​​​Size:499418034176, ​​​​​​​​​​​​​Content:"Apple_CoreStorage"​​​​​​​​​​​}, ​​​​​​​​​​​{​​​​​​​​​​​​​DiskUUID:"EE72FC16-C614-4C77-9048-6EF118451D48", ​​​​​​​​​​​​​DeviceIdentifier:"disk0s3", ​​​​​​​​​​​​​Size:650002432, ​​​​​​​​​​​​​Content:"Apple_Boot", ​​​​​​​​​​​​​VolumeName:"Recovery HD", ​​​​​​​​​​​​​VolumeUUID:"3400E36B-24AA-3B7A-93E3-BBDCDFF48548"​​​​​​​​​​​}​​​​​​​​​}​​​​​​​}, ​​​​​​​{​​​​​​​​​DeviceIdentifier:"disk1", ​​​​​​​​​Size:499055067136, ​​​​​​​​​MountPoint:"/", ​​​​​​​​​Content:"Apple_HFS", ​​​​​​​​​VolumeName:"Cherry"​​​​​​​}​​​​​}, ​​​​​WholeDisks:{​​​​​​​"disk0", ​​​​​​​"disk1"​​​​​}, ​​​​​VolumesFromDisks:{​​​​​​​"Cherry"​​​​​}​​​}

— extract relevant info
set disksAndParitions to pListDict’s objectForKey:"AllDisksAndPartitions"
–>  (NSArray) {​​​​​{​​​​​​​DeviceIdentifier:"disk0", ​​​​​​​Size:500277790720, ​​​​​​​Content:"GUID_partition_scheme", ​​​​​​​Partitions:{​​​​​​​​​{​​​​​​​​​​​DiskUUID:"FC89778D-9028-4FAA-8118-F74802B0664E", ​​​​​​​​​​​DeviceIdentifier:"disk0s1", ​​​​​​​​​​​Size:209715200, ​​​​​​​​​​​Content:"EFI", ​​​​​​​​​​​VolumeName:"EFI", ​​​​​​​​​​​VolumeUUID:"85D67001-D93E-3687-A1C2-79D677F0C2E0"​​​​​​​​​}, ​​​​​​​​​{​​​​​​​​​​​DiskUUID:"AAF54C8C-9229-4DC1-8EA5-5FCCE9050DC4", ​​​​​​​​​​​DeviceIdentifier:"disk0s2", ​​​​​​​​​​​Size:499418034176, ​​​​​​​​​​​Content:"Apple_CoreStorage"​​​​​​​​​}, ​​​​​​​​​{​​​​​​​​​​​DiskUUID:"EE72FC16-C614-4C77-9048-6EF118451D48", ​​​​​​​​​​​DeviceIdentifier:"disk0s3", ​​​​​​​​​​​Size:650002432, ​​​​​​​​​​​Content:"Apple_Boot", ​​​​​​​​​​​VolumeName:"Recovery HD", ​​​​​​​​​​​VolumeUUID:"3400E36B-24AA-3B7A-93E3-BBDCDFF48548"​​​​​​​​​}​​​​​​​}​​​​​}, ​​​​​{​​​​​​​DeviceIdentifier:"disk1", ​​​​​​​Size:499055067136, ​​​​​​​MountPoint:"/", ​​​​​​​Content:"Apple_HFS", ​​​​​​​VolumeName:"Cherry"​​​​​}​​​}

set partitionsArray to current application’s NSMutableArray’s array() — to store values
repeat with anEntry in disksAndParitions
  set thePartitions to (anEntry’s objectForKey:"Partitions")
  
if thePartitions = missing value then — no partitions means a volume
    (partitionsArray’s addObject:anEntry)
  else
    (partitionsArray’s addObjectsFromArray:thePartitions)
  end if
end repeat

— filter by Content type
set thePred to current application’s NSPredicate’s predicateWithFormat:"Content == ’Apple_HFS’ OR Content == ’Apple_APFS’ "
set partitionsArray to partitionsArray’s filteredArrayUsingPredicate:thePred

— get names
return (partitionsArray’s valueForKey:"VolumeName") as list

★Click Here to Open This Script 

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

指定のDiskの情報を取得する

Posted on 2月 20, 2018 by Takaaki Naganoya
AppleScript名:指定のDiskの情報を取得する
— Created 2016-04-06 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set targDev to "/dev/disk3" –Blu-ray drive via USB

set aDict to current application’s NSMutableDictionary’s alloc()’s init()
try
  set a to do shell script "diskutil info " & targDev
on error
  return false
end try

set aList to paragraphs of a

repeat with i in aList
  set j to contents of i
  
if j is not equal to "" then
    set jj to parseByDelim(j, ":") of me
    
    
if (count every item of jj) is equal to 2 then
      copy jj to {j2, j3}
      
      
set jj2 to (current application’s NSString’s stringWithString:j2)
      
set jj3 to (current application’s NSString’s stringWithString:j3)
      
      
set jjj2 to (jj2’s stringByTrimmingCharactersInSet:(current application’s NSCharacterSet’s whitespaceCharacterSet()))
      
set jjj3 to (jj3’s stringByTrimmingCharactersInSet:(current application’s NSCharacterSet’s whitespaceCharacterSet()))
      
      (
aDict’s setValue:jjj3 forKey:jjj2)
    end if
    
  end if
end repeat

return aDict as list of string or string
–>  {Read-Only Media:"Yes", OS Can Be Installed:"No", Volume Name:"Not applicable (no file system)", OS 9 Drivers:"No", Virtual:"No", Device Block Size:"2048 Bytes", Low Level Format:"Not supported", Removable Media:"Yes", Device / Media Name:"MATSHITA BD-MLT UJ240AS", Optical Media Erasable:"No", Optical Drive Type:"CD-ROM, CD-R, CD-RW, DVD-ROM, DVD-R, DVD-R DL, DVD-RW, DVD-RAM, DVD+R, DVD+R DL, DVD+RW, BD-ROM, BD-R, BD-RE", Part of Whole:"disk3", Device Identifier:"disk3", Whole:"Yes", Optical Media Type:"BD-R", SMART Status:"Not Supported", Device Location:"External", Volume Free Space:"Not applicable (no file system)", Device Node:"/dev/disk3", Read-Only Volume:"Not applicable (no file system)", Media Type:"Generic", Total Size:"0 B (0 Bytes) (exactly 0 512-Byte-Units)", Media Removal:"Software-Activated", Mounted:"Not applicable (no file system)", Content (IOContent):"None", File System:"None", Protocol:"USB"}

–テキストを指定デリミタでリスト化
on parseByDelim(aData, aDelim)
  set aText to current application’s NSString’s stringWithString:aData
  
set aList to aText’s componentsSeparatedByString:aDelim
  
return aList as list
end parseByDelim

★Click Here to Open This Script 

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

Input Methodのメニュー表示名を取得する

Posted on 2月 20, 2018 by Takaaki Naganoya

–> InputManager.framework

AppleScript名:Input Methodのメニュー表示名を取得する
— Created 2017-01-22 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "InputManager" –https://github.com/jensnockert/input-manager

set cList to (current application’s CSInputSource’s all()’s valueForKey:"localizedName") as list
–>  {​​​​​"ひらがな", ​​​​​"英字", ​​​​​"カタカナ", ​​​​​"日本語", ​​​​​"かなパレット", ​​​​​"com.apple.PressAndHold", ​​​​​"絵文字と記号", ​​​​​"キーボードビューア", ​​​​​"EmojiFunctionRowIM_Extension"​​​}–10.10.x
–> {"ひらがな", "英字", "カタカナ", "日本語", "かなパレット", "com.apple.PressAndHold", "絵文字と記号", "キーボードビューア"}–10.12.x

★Click Here to Open This Script 

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

InputManagerでIMを切り換える

Posted on 2月 20, 2018 by Takaaki Naganoya

InputManager.frameworkを呼び出して、日本語入力Input Methodの入力文字の切り替えを行うAppleScriptです。

スクリプトエディタ上でControl-Command-Rにてフロントエンド・プロセスで実行する必要があります。

–> Demo Movie

–> InputManager.framework

AppleScript名:InputManagerでIMを切り換える
— Created 2017-01-22 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"
use framework "InputManager" –https://github.com/jensnockert/input-manager

–Check If this script runs in foreground
if not (current application’s NSThread’s isMainThread()) as boolean then
  display alert "This script must be run from the main thread (Command-Control-R in Script Editor)." buttons {"Cancel"} as critical
  
error number -128
end if

–Caution: This script runs on only Japanese language environment

set cList to current application’s CSInputSource’s all()
set dList to (cList’s valueForKey:"localizedName")
set aRes to ((dList’s indexOfObject:"ひらがな") as integer) –"Hiragana" in Japanese
set bRes to ((dList’s indexOfObject:"英字") as integer) –"English Letters" in Japanese

set hiraKey to cList’s objectAtIndex:aRes
set engKey to cList’s objectAtIndex:bRes

repeat 10 times
  hiraKey’s |select|()
  
delay 1
  
engKey’s |select|()
  
delay 1
end repeat

★Click Here to Open This Script 

Posted in Input Method Require Control-Command-R to run System | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

display情報の取得

Posted on 2月 20, 2018 by Takaaki Naganoya
AppleScript名:display情報の取得
— Created 2015-01-13 by Takaaki Naganoya
— 2015 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

–Retina Displayを含む構成のとき(macOS 10.10.x)
set dInfoList to retScreenInfos()
–>  {{screenSize:{{width:1920.0, height:1200.0}}, screenResol:{{width:144.0, height:144.0}}, screenIsScreen:true, screenNumber:"69501832", screenColorSpace:"NSCalibratedRGBColorSpace", screenColDepth:"8"}, {screenSize:{{width:1920.0, height:1200.0}}, screenResol:{{width:144.0, height:144.0}}, screenIsScreen:true, screenNumber:"69513475", screenColorSpace:"NSCalibratedRGBColorSpace", screenColDepth:"8"}, {screenSize:{{width:1920.0, height:1200.0}}, screenResol:{{width:144.0, height:144.0}}, screenIsScreen:true, screenNumber:"69731202", screenColorSpace:"NSCalibratedRGBColorSpace", screenColDepth:"8"}, {screenSize:{{width:1920.0, height:1080.0}}, screenResol:{{width:72.0, height:72.0}}, screenIsScreen:true, screenNumber:"458586661", screenColorSpace:"NSCalibratedRGBColorSpace", screenColDepth:"8"}}

–Retina Displayを含む構成のとき(macOS 10.12.6)
–> {{screenSize:{{width:1920.0, height:1200.0}}, screenResol:{{width:144.0, height:144.0}}, screenIsScreen:true, screenNumber:"69731202", screenColorSpace:"NSCalibratedRGBColorSpace", screenColDepth:"8"}, {screenSize:{{width:1920.0, height:1080.0}}, screenResol:{{width:72.0, height:72.0}}, screenIsScreen:true, screenNumber:"458586661", screenColorSpace:"NSCalibratedRGBColorSpace", screenColDepth:"8"}}

–Retina Displayを含まない構成のとき(MacBook Pro Retina本体のLid Closed Mode時)(macOS 10.10.x)
–set dInfoList to retScreenInfos()
–>  {{screenSize:{{width:1920.0, height:1200.0}}, screenResol:{{width:72.0, height:72.0}}, screenIsScreen:true, screenNumber:"69501832", screenColorSpace:"NSCalibratedRGBColorSpace", screenColDepth:"8"}, {screenSize:{{width:1920.0, height:1200.0}}, screenResol:{{width:72.0, height:72.0}}, screenIsScreen:true, screenNumber:"69513475", screenColorSpace:"NSCalibratedRGBColorSpace", screenColDepth:"8"}, {screenSize:{{width:1920.0, height:1080.0}}, screenResol:{{width:72.0, height:72.0}}, screenIsScreen:true, screenNumber:"458586661", screenColorSpace:"NSCalibratedRGBColorSpace", screenColDepth:"8"}}

–Retina Displayを含まない構成のとき(MacBook Pro Retina本体のLid Closed Mode時)(macOS 10.12.6)
–> {{screenSize:{{width:1920.0, height:1200.0}}, screenResol:{{width:72.0, height:72.0}}, screenIsScreen:true, screenNumber:"69501831", screenColorSpace:"NSCalibratedRGBColorSpace", screenColDepth:"8"}, {screenSize:{{width:1920.0, height:1200.0}}, screenResol:{{width:72.0, height:72.0}}, screenIsScreen:true, screenNumber:"69513476", screenColorSpace:"NSCalibratedRGBColorSpace", screenColDepth:"8"}, {screenSize:{{width:1920.0, height:1080.0}}, screenResol:{{width:72.0, height:72.0}}, screenIsScreen:true, screenNumber:"458586661", screenColorSpace:"NSCalibratedRGBColorSpace", screenColDepth:"8"}}

on retScreenInfos()
  set sList to (current application’s NSScreen’s screens()) as list
  
  
set dList to {}
  
repeat with i in sList
    set a to i’s deviceDescription()
    
set aSize to a’s NSDeviceSize as list
    
set aResol to a’s NSDeviceResolution as list
    
set aScrn to a’s NSDeviceIsScreen as boolean
    
set aNum to a’s NSScreenNumber as string
    
set aColSpc to a’s NSDeviceColorSpaceName as string
    
set aColDepth to a’s NSDeviceBitsPerSample as string
    
set the end of dList to {screenSize:aSize, screenResol:aResol, screenIsScreen:aScrn, screenNumber:aNum, screenColorSpace:aColSpc, screenColDepth:aColDepth}
  end repeat
  
  
return dList
end retScreenInfos

★Click Here to Open This Script 

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

Lock Screen

Posted on 2月 20, 2018 by Takaaki Naganoya

MacをLock Screenの状態にするAppleScriptです。

Lock Screenの状態では、実行中の他のAppleScriptはそのまま動作し続けます。音声出力はオフになります。

ただし、Lock Screen表示はそのまま表示され続けるため、画面表示をオフにしたいだけという用途には合っていません。

AppleScript名:ASOCでLock Screen
— Created 2015-09-11 by Takaaki Naganoya
— 2015 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aTask to current application’s NSTask’s alloc()’s init()
aTask’s setLaunchPath:"/System/Library/CoreServices/Menu Extras/User.menu/Contents/Resources/CGSession"
aTask’s setArguments:(current application’s NSArray’s arrayWithObject:"-suspend")
aTask’s |launch|()

★Click Here to Open This Script 

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

画面情報を取得する

Posted on 2月 20, 2018 by Takaaki Naganoya
AppleScript名:画面情報を取得する
use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

set allInfo to {}
set theScreens to current application’s NSScreen’s screens()
repeat with aScreen in theScreens
  set theInfo to aScreen’s deviceDescription() as record
  
set theInfo to theInfo & aScreen’s frame() as record
  
set end of allInfo to theInfo
end repeat
return allInfo
–> {{NSDeviceResolution:{width:144.0, height:144.0}, NSDeviceSize:{width:1920.0, height:1200.0}, NSDeviceIsScreen:"YES", NSScreenNumber:2.077752445E+9, NSDeviceColorSpaceName:"NSCalibratedRGBColorSpace", NSDeviceBitsPerSample:8, origin:{x:0.0, y:0.0}, |size|:{width:1920.0, height:1200.0}}}

–> {{NSDeviceResolution:{width:144.0, height:144.0}, NSDeviceSize:{width:1920.0, height:1200.0}, NSDeviceIsScreen:"YES", NSScreenNumber:69731202, NSDeviceColorSpaceName:"NSCalibratedRGBColorSpace", NSDeviceBitsPerSample:8, origin:{x:0.0, y:0.0}, |size|:{width:1920.0, height:1200.0}}, {NSDeviceResolution:{width:72.0, height:72.0}, NSDeviceSize:{width:1920.0, height:1080.0}, NSDeviceIsScreen:"YES", NSScreenNumber:458586661, NSDeviceColorSpaceName:"NSCalibratedRGBColorSpace", NSDeviceBitsPerSample:8, origin:{x:1920.0, y:216.0}, |size|:{width:1920.0, height:1080.0}}}

★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
AppleScript名:使用メモリーの状況を取得
— Created 2017-12-17 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set mRes to retMemoryUsage() of me
–> {usedMem:7891, wiredMem:2220, unusedMem:293}

on retMemoryUsage()
  set memRes to do shell script "top -l 1 | head -10 | grep ’PhysMem’"
  
set aRes to (parseStrFromTo(memRes, " ", "M") of me)
  
  
set bList to {}
  
repeat with i in aRes
    set mRes to returnNumberOnly(i) of me
    
set the end of bList to mRes
  end repeat
  
  
set usedNum to contents of first item of bList
  
set wiredNum to contents of second item of bList
  
set unusedNum to contents of third item of bList
  
return {usedMem:usedNum as integer, wiredMem:wiredNum as integer, unusedMem:unusedNum as integer}
end retMemoryUsage

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

on returnNumberOnly(aStr)
  set anNSString to current application’s NSString’s stringWithString:aStr
  
set anNSString to anNSString’s stringByReplacingOccurrencesOfString:"[^0-9]" withString:"" options:(current application’s NSRegularExpressionSearch) range:{0, anNSString’s |length|()}
  
return anNSString as text
end returnNumberOnly

★Click Here to Open This Script 

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

電池容量(SPEC)を取得

Posted on 2月 20, 2018 by Takaaki Naganoya
AppleScript名:電池容量(SPEC)を取得
–バッテリー容量(Design Capacity)
do shell script "ioreg -l | grep DesignCapacity | sed ’s/^.*DesignCapacity\" = \\([0-9]*\\)$/\\1/g’"

★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
AppleScript名:バッテリ充放電の回数を取得する
–バッテリ充放電回数
set cycle to (do shell script "ioreg -l | grep ’\"CycleCount\"’ | sed ’s/^.*CycleCount. = \\([0-9]*\\)$/\\1/g’")

★Click Here to Open This Script 

macOS 10.14.6上で数値だけが返ってくるわけではなかったので、泥縄式にこんな風に書き換えて回数の数値だけえられるようにしてみました。

AppleScript名:バッテリの充放電回数を取得する v2
–バッテリ充放電回数
try
  set cycle to (do shell script "ioreg -l | grep ’\"CycleCount\"’ | sed ’s/^.*CycleCount. = \\([0-9]*\\)$/\\1/g’")
  
set cRes to first item of (paragraphs of cycle) as number
  
return cRes
on error
  return false
end try

★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
AppleScript名:バッテリの容量を取得する
–バッテリ容量
set cap to (do shell script "ioreg -l | grep MaxCapacity | sed ’s/^.*MaxCapacity\" = \\([0-9]*\\)$/\\1/g’") as {number, anything}

★Click Here to Open This Script 

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

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

Post navigation

  • Older posts
  • Newer posts

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

Google Search

Popular posts

  • macOS 13, Ventura(継続更新)
  • アラートダイアログ上にWebViewで3Dコンテンツを表示(WebGL+three.js)v3
  • Xcode 14.2でAppleScript App Templateを復活させる
  • macOS 13 TTS Voice環境に変更
  • UI Browserがgithub上でソース公開され、オープンソースに
  • 2022年に書いた価値あるAppleScript
  • ChatGPTで文章のベクトル化(Embedding)
  • iWork 12.2がリリースされた
  • 新発売:CotEditor Scripting Book with AppleScript
  • macOS 13対応アップデート:AppleScript実践的テクニック集(1)GUI Scripting
  • ChatGPTでchatに対する応答文を取得
  • macOS 13でNSNotFoundバグふたたび
  • 新発売:iWork Scripting Book with AppleScript
  • Finderの隠し命令openVirtualLocationが発見される
  • macOS 13.1アップデートでスクリプトエディタの挙動がようやくまともに
  • あのコン過去ログビューワー(暫定版)
  • AppleScriptの数値変数で指数表示にならない最大値、最小値
  • バカスタム App選手権 入賞!
  • Dockアイコンにプログレスバーを追加 v3
  • macOS 14, Sonoma

Tags

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

カテゴリー

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

アーカイブ

  • 2023年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