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月

指定写真をQRコード認識してエンコードされたデータをデコード

Posted on 2月 8, 2018 by Takaaki Naganoya

–> {“MEMORY:(メモ文字列)
NAME1:(姓名)
NAME2:(姓名読み)
TEL1:(電話番号1)
MAIL1:(メールアドレス1)
TEL2:(電話番号2)
MAIL2:(メールアドレス2)
MECARD:N:(姓),(名);SOUND:(姓読み),(名読み);TEL:(電話番号1);TEL:(電話番号2);EMAIL:(メールアドレス1);EMAIL:(メールアドレス2);NOTE:(メモ文字列) ;;”}

AppleScript名:指定写真をQRコード認識してエンコードされたデータをデコード
use AppleScript version "2.4"
use framework "Foundation"
use framework "QuartzCore"
use scripting additions

–画像選択
set inputFile to choose file of type {"public.image"}

–QRコード検出
set recogRes to qrcodeDetect(inputFile) of me

–>
(*
{"http://www.su-gomori.com \nMEBKM:TITLE:スゴモリ;URL:http¥://www.su-gomori.com;;"}
*)

on qrcodeDetect(inputFile)
  –画像オープン
  
set imageRef to openImageFile(inputFile)
  
  
— 検出器のオプションを NSDictonary で作成
  
set optDic1 to current application’s NSDictionary’s dictionaryWithObject:(current application’s CIDetectorAccuracyHigh) forKey:(current application’s CIDetectorAccuracy)
  
set faceDetector to current application’s CIDetector’s detectorOfType:(current application’s CIDetectorTypeQRCode) context:(missing value) options:optDic1
  
  
— QRコードの検出を行う際のオプションを NSDictonary で作成
  
set optDic2 to current application’s NSDictionary’s dictionaryWithObject:(current application’s CIDetectorImageOrientation) forKey:"Orientation"
  
  
— QRコード検出を実行
  
set faceArray to faceDetector’s featuresInImage:imageRef options:optDic2
  
set fList to {}
  
  
— 検出されたQRコードの位置とサイズをログに出力
  
repeat with i from 1 to (count of faceArray)
    set face to item i of faceArray
    
set bRec to (face’s messageString()) as string
    
–set cRec to retURLdecodedStrings(bRec) of me –URLエンコード対策
    
set the end of fList to bRec
  end repeat
  
  
return fList
  
end qrcodeDetect

on openImageFile(imageFile) — imageFile: POSIX path 形式のファイルパス
  — aliasをURLに変換
  
set fileURL to current application’s |NSURL|’s fileURLWithPath:(POSIX path of imageFile)
  
— CIImage を生成
  
return current application’s CIImage’s alloc()’s initWithContentsOfURL:fileURL
end openImageFile

on retURLencodedStrings(aText)
  set aStr to current application’s NSString’s stringWithString:aText
  
set encodedStr to aStr’s stringByAddingPercentEncodingWithAllowedCharacters:(current application’s NSCharacterSet’s alphanumericCharacterSet())
  
return encodedStr as text
end retURLencodedStrings

on retURLdecodedStrings(aURLencodedStr)
  set aStr to current application’s NSString’s stringWithString:aURLencodedStr
  
set aDecoded to aStr’s stringByRemovingPercentEncoding()
  
return aDecoded as text
end retURLdecodedStrings

★Click Here to Open This Script 

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

指定文字列からQRコード画像(PNG)をデスクトップに作成する(日本語を含むデータ)v2_画像拡大倍率指定

Posted on 2月 8, 2018 by Takaaki Naganoya

AppleScript名:指定文字列からQRコード画像(PNG)をデスクトップに作成する(日本語を含むデータ)v2_画像拡大倍率指定
— Created 2016-03-16 by Takaaki Naganoya
— Modified 2017-01-15 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"
use framework "QuartzCore"

–QRCodeを作成
set a to "MEMORY:(メモ文字列)
NAME1:(姓名)
NAME2:(姓名読み)
TEL1:(電話番号1)
MAIL1:(メールアドレス1)
TEL2:(電話番号2)
MAIL2:(メールアドレス2)
MECARD:N:(姓),(名);SOUND:(姓読み),(名読み);TEL:(電話番号1);TEL:(電話番号2);EMAIL:(メールアドレス1);EMAIL:(メールアドレス2);NOTE:(メモ文字列) ;;"

set aStr to current application’s NSString’s stringWithString:a
set strData to aStr’s dataUsingEncoding:(current application’s NSShiftJISStringEncoding) –シフトJISにエンコード
set qrFilter to current application’s CIFilter’s filterWithName:"CIQRCodeGenerator"
qrFilter’s setValue:strData forKey:"inputMessage"
qrFilter’s setValue:"H" forKey:"inputCorrectionLevel"
set anImage to qrFilter’s outputImage()

set convImg to convCIimageToNSImage(anImage) of me

–NSImageを拡大(アンチエイリアス解除で)
set resizedImg to my resizeNSImageWithoutAntlialias:convImg toScale:16.0

–デスクトップに保存
set aDesktopPath to (current application’s NSProcessInfo’s processInfo()’s environment()’s objectForKey:("HOME"))’s stringByAppendingString:"/Desktop/"
set savePath to aDesktopPath’s stringByAppendingString:((current application’s NSUUID’s UUID()’s UUIDString())’s stringByAppendingString:".png")
saveNSImageAtPathAsPNG(resizedImg, savePath) of me

on convCIimageToNSImage(aCIImage)
  set aRep to current application’s NSBitmapImageRep’s alloc()’s initWithCIImage:aCIImage
  
set tmpSize to aRep’s |size|()
  
set newImg to current application’s NSImage’s alloc()’s initWithSize:tmpSize
  
newImg’s addRepresentation:aRep
  
return newImg
end convCIimageToNSImage

on convNSImageToCIimage(aNSImage)
  set tiffDat to aNSImage’s TIFFRepresentation()
  
set aRep to current application’s NSBitmapImageRep’s imageRepWithData:tiffDat
  
set newImg to current application’s CIImage’s alloc()’s initWithBitmapImageRep:aRep
  
return newImg
end convNSImageToCIimage

–NSImageを指定パスにPNG形式で保存
on saveNSImageAtPathAsPNG(anImage, outPath)
  set imageRep to anImage’s TIFFRepresentation()
  
set aRawimg to current application’s NSBitmapImageRep’s imageRepWithData:imageRep
  
set pathString to current application’s NSString’s stringWithString:outPath
  
set newPath to pathString’s stringByExpandingTildeInPath()
  
set myNewImageData to (aRawimg’s representationUsingType:(current application’s NSPNGFileType) |properties|:(missing value))
  
set aRes to (myNewImageData’s writeToFile:newPath atomically:true) as boolean
  
return aRes –成功ならtrue、失敗ならfalseが返る
end saveNSImageAtPathAsPNG

–NSImageを指定倍率で拡大(アンチエイリアス解除状態で)–By Shane Stanley
on resizeNSImageWithoutAntlialias:aSourceImg toScale:imgScale
  set aSize to aSourceImg’s |size|()
  
set aWidth to (aSize’s width) * imgScale
  
set aHeight to (aSize’s height) * imgScale
  
  
set aRep to current application’s NSBitmapImageRep’s alloc()’s initWithBitmapDataPlanes:(missing value) pixelsWide:aWidth pixelsHigh:aHeight bitsPerSample:8 samplesPerPixel:4 hasAlpha:true isPlanar:false colorSpaceName:(current application’s NSCalibratedRGBColorSpace) bytesPerRow:0 bitsPerPixel:0
  
  
set newSize to {width:aWidth, height:aHeight}
  
aRep’s setSize:newSize
  
  
current application’s NSGraphicsContext’s saveGraphicsState()
  
  
set theContext to current application’s NSGraphicsContext’s graphicsContextWithBitmapImageRep:aRep
  
current application’s NSGraphicsContext’s setCurrentContext:theContext
  
theContext’s setShouldAntialias:false
  
theContext’s setImageInterpolation:(current application’s NSImageInterpolationNone)
  
  
aSourceImg’s drawInRect:(current application’s NSMakeRect(0, 0, aWidth, aHeight)) fromRect:(current application’s NSZeroRect) operation:(current application’s NSCompositeCopy) fraction:(1.0)
  
  
current application’s NSGraphicsContext’s restoreGraphicsState()
  
  
set newImg to current application’s NSImage’s alloc()’s initWithSize:newSize
  
newImg’s addRepresentation:aRep
  
  
return newImg
end resizeNSImageWithoutAntlialias:toScale:

★Click Here to Open This Script 

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

Finder上で選択中の画像を横方向に連結

Posted on 2月 8, 2018 by Takaaki Naganoya

–> MagicKit.framework

AppleScript名:Finder上で選択中の画像を横方向に連結
— Created 2017-11-21 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "QuartzCore"
use framework "AppKit"
use framework "MagicKit" –https://github.com/aidansteele/magickit

property |NSURL| : a reference to current application’s |NSURL|
property NSUUID : a reference to current application’s NSUUID
property NSArray : a reference to current application’s NSArray
property NSString : a reference to current application’s NSString
property NSImage : a reference to current application’s NSImage
property GEMagicKit : a reference to current application’s GEMagicKit
property NSPNGFileType : a reference to current application’s NSPNGFileType
property NSMutableArray : a reference to current application’s NSMutableArray
property NSBitmapImageRep : a reference to current application’s NSBitmapImageRep

property xGap : 10 –連結時の画像間のアキ(横方向)

tell application "Finder"
  set aSel to selection as alias list
  
if aSel = {} or aSel = "" then return
end tell

–選択した画像をArrayに入れる
set imgList to NSMutableArray’s new()
repeat with i in aSel
  set aPath to POSIX path of i
  
  
–指定ファイルのUTIを取得して、画像(public.image)があれば処理を行う
  
set aRes to (GEMagicKit’s magicForFileAtPath:aPath)
  
set utiList to (aRes’s uniformTypeHierarchy()) as list
  
if "public.image" is in utiList then
    set aNSImage to (NSImage’s alloc()’s initWithContentsOfFile:aPath)
    (
imgList’s addObject:aNSImage)
  end if
end repeat

–KVCで画像の各種情報をまとめて取得
set sizeList to (imgList’s valueForKeyPath:"size") as list –NSSize to list of record conversion
set maxHeight to ((NSArray’s arrayWithArray:sizeList)’s valueForKeyPath:"@max.height") as real
set totalWidth to ((NSArray’s arrayWithArray:sizeList)’s valueForKeyPath:"@sum.width") as real
set totalCount to ((NSArray’s arrayWithArray:sizeList)’s valueForKeyPath:"@count") as integer

–出力画像作成
set tSize to current application’s NSMakeSize((totalWidth + (xGap * totalCount)), maxHeight)
set newImage to NSImage’s alloc()’s initWithSize:tSize

–順次画像を新規画像に上書き
set xOrig to 0
repeat with i in (imgList as list)
  set j to contents of i
  
set curSize to j’s |size|()
  
set aRect to {xOrig, (maxHeight – (curSize’s height())), (curSize’s width()), (curSize’s height())}
  
set newImage to composeImage(newImage, j, aRect) of me
  
set xOrig to (curSize’s width()) + xGap
end repeat

–デスクトップにPNG形式でNSImageをファイル保存
set aDesktopPath to current application’s NSHomeDirectory()’s stringByAppendingString:"/Desktop/"
set savePath to aDesktopPath’s stringByAppendingString:((NSUUID’s UUID()’s UUIDString())’s stringByAppendingString:".png")
set fRes to saveNSImageAtPathAsPNG(newImage, savePath) of me

–2つのNSImageを重ね合わせ合成してNSImageで返す
on composeImage(backImage, composeImage, aTargerRect)
  set newImage to NSImage’s alloc()’s initWithSize:(backImage’s |size|())
  
  
copy aTargerRect to {x1, y1, x2, y2}
  
set bRect to current application’s NSMakeRect(x1, y1, x2, y2)
  
  
newImage’s lockFocus()
  
  
set newImageRect to current application’s CGRectZero
  
set newImageRect’s |size| to (newImage’s |size|)
  
  
backImage’s drawInRect:newImageRect
  
composeImage’s drawInRect:bRect
  
  
newImage’s unlockFocus()
  
return newImage
end composeImage

–NSImageを指定パスにPNG形式で保存
on saveNSImageAtPathAsPNG(anImage, outPath)
  set imageRep to anImage’s TIFFRepresentation()
  
set aRawimg to NSBitmapImageRep’s imageRepWithData:imageRep
  
set pathString to NSString’s stringWithString:outPath
  
set newPath to pathString’s stringByExpandingTildeInPath()
  
set myNewImageData to (aRawimg’s representationUsingType:(NSPNGFileType) |properties|:(missing value))
  
set aRes to (myNewImageData’s writeToFile:newPath atomically:true) as boolean
  
return aRes –成功ならtrue、失敗ならfalseが返る
end saveNSImageAtPathAsPNG

★Click Here to Open This Script 

Posted in Image | Tagged 10.11savvy 10.12savvy 10.13savvy Finder | Leave a comment

Get the ISO 3166 country code from Time Zone

Posted on 2月 8, 2018 by Takaaki Naganoya

–> contryAndLocationKit.framework

AppleScript名:Get the ISO 3166 country code from Time Zone
— Created 2016-04-10 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "contryAndLocationKit" –iospirit/NSTimeZone-ISCLLocation & BigZaphod/CLLocation-SunriseSunset
–https://github.com/iospirit/NSTimeZone-ISCLLocation
–https://github.com/BigZaphod/CLLocation-SunriseSunset

set aTZ to (current application’s NSTimeZone’s localTimeZone())’s |description|() as string
–>"Local Time Zone (Asia/Tokyo (JST) offset 32400)"

set aCountry to ((current application’s NSTimeZone’s localTimeZone())’s ISO3166CountryCode()) as list of string or string
–>  "JP"
–> missing value (error)

★Click Here to Open This Script 

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

1箇所から別の箇所の方位を求める

Posted on 2月 8, 2018 by Takaaki Naganoya

任意の緯度・経度情報から、別の箇所の緯度・経度情報の「方位」を計算するAppleScriptです。

北を0として、西に向かうとマイナス、東に向かうとプラスの値で角度(方位)を返します。この手の計算に必須のatan2関数がAppleScriptに標準装備されていないため、atan2の機能を提供するSatimage OSAXを実行環境にインストールしておく必要があります。

各種三角関数に関しては、直接計算する関数がなくても、あらかじめ計算しておいてテーブルから取得する程度でも精度的には問題のないケースが多いので、テーブルでデータを持っておくことも多々あります。

最近、Numbersで内蔵関数を増やすために利用している「cephes math library」あたりがCレベルではなくObjective-Cレベルから直接利用できるとよさそうなのですが、、、

AppleScript名:1箇所から別の箇所の方位を求める
— Created 2017-04-27 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions –requires "Satimage osax" http://www.satimage.fr/software/en/downloads/downloads_companion_osaxen.html
use framework "Foundation"
use bPlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html
–http://zurb.com/forrst/posts/Direction_between_2_CLLocations-uAo

set coord1 to {latitude:35.73677496, longitude:139.63754457} –中村橋駅
set coord2 to {latitude:35.78839012, longitude:139.61241447} –和光市駅
set dirRes to calcDirectionBetweenTwoPlaces(coord1, coord2) of me
–>  -25.925429877542

(*)
set coord2 to {latitude:35.78839012, longitude:139.61241447} –和光市駅
set dirRes to calcDirectionBetweenTwoPlaces(coord1, coord2) of me
–>  -25.925429877542

set coord2 to {latitude:35.7227821, longitude:139.63860897} –鷺宮駅
set dirRes to calcDirectionBetweenTwoPlaces(coord1, coord2) of me
–>  175.649833487804

set coord2 to {latitude:35.73590542, longitude:139.62986745} –富士見台駅
set dirRes to calcDirectionBetweenTwoPlaces(coord1, coord2) of me
–>  -96.385293928667

set coord2 to {latitude:35.73785024, longitude:139.65339321} –練馬駅
set dirRes to calcDirectionBetweenTwoPlaces(coord1, coord2) of me
–>  85.959474671834

set coord2 to {latitude:35.71026838, longitude:139.81215754} –東京スカイツリー
set dirRes to calcDirectionBetweenTwoPlaces(coord1, coord2) of me
–>  96.826542737106
*)

–位置情報1から位置情報2の方角を計算。北が0度
on calcDirectionBetweenTwoPlaces(coord1, coord2)
  load framework
  
set deltaLong to (longitude of coord2) – (longitude of coord1)
  
set yComponent to bPlus’s sinValueOf:deltaLong
  
set xComponent to (bPlus’s cosValueOf:(latitude of coord1)) * (bPlus’s sinValueOf:(latitude of coord2)) – (bPlus’s sinValueOf:(latitude of coord1)) * (bPlus’s cosValueOf:(latitude of coord2)) * (bPlus’s cosValueOf:deltaLong)
  
  
set vList to {yComponent, xComponent}
  
set radians to atan2 vList —Requires SatImage OSAX
  
set degreeRes to (radToDeg(radians) of me)
  
  
return degreeRes
end calcDirectionBetweenTwoPlaces

on radToDeg(aRadian)
  return aRadian * (180 / pi)
end radToDeg

★Click Here to Open This Script 

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

都道府県リストから隣接都道府県を含む該当のコードを抽出する

Posted on 2月 8, 2018 by Takaaki Naganoya

膨大な位置情報の距離を計算するような場合に、対象の都道府県と隣接する都道府県に限定して距離計算を行うことで計算量を削減するためのAppleScriptです。

ある1点の位置情報に対して大量の地点との距離計算を行う処理は割とよくあります。自宅からの最寄駅の計算や、日本全国に点在している施設の最寄駅の計算などなどです。

たいていは、総当たりですべての計算対象との距離計算を行い、近い順でソートすることになります。自分の場合では日本国内の700か所ぐらいの(「戦場の絆」が設置されている)ゲームセンターの位置と日本全国の鉄道の駅(8,000か所程度)を比較して最寄駅を計算しました。

700 x 8,000 = 5,600,000回の距離計算を行うわけで、あまりに時間がかかるのでめまいがします(90分ぐらいかかりました)。1か所あたりの距離計算の所用時間は0.086秒程度なのでいちがいに遅いとはいえません(CocoaのCoreLocationの機能を使わないで、地球を球としてみなして近似的な距離計算を行えば速くなることでしょう)。

ここで、脳みそが頭に入っている人間であれば考えるわけです。もっと速く計算できる方法があるんじゃないか? と。

だいたい、北海道にある地点の最寄駅を求めるのに九州の最果てにある駅との距離計算を行う必要はありません。8,000箇所のすべての駅との距離計算を行う中には、膨大な「無意味な計算」が含まれています。この、「無意味な計算」を除外してあげれば、計算量が半分で済む場合には計算時間は半分になります。

そうやって考えていくと、都道府県レベルで隣接している地点については計算を行う必要がありますが、隣接していない都道府県にある駅との計算は行う意味がないことに気づきます。

そして実際に本テーブルを作成して計算に導入したところ、当初の90分が10分に短縮されました。さらに、データの持ち方を変えて最終的には5分ぐらいまで短縮できました。

「都道府県レベルで計算対象の除外ができて、高速処理を実現できたんだから、市区町村レベルの隣接情報を作って計算すれば、さらに高速化できるのでは?」

と考える人がいるかもしれません。ただ、その処理を実際に頭の中でシミュレーションしてみると、市区町村レベルでは「駅」が存在していないものもあったりで、日本国内に3,0000程度あるとみられるそのレベルの地方公共団体の隣接データの作成は大変です。たしかに高速化できるかもしれませんが、その成果は微々たるものになることでしょう(東京、名古屋、大阪などの大都市にかぎっては効果があるかも? その他の地域ではさほど効果はありません)。

対象は日本国内の都道府県。海外の住所情報でも同様に都道府県にコードを振って隣接情報をデータ化すれば同様の処理は可能です。

# BridgePlus Script Libraryを併用しなくてもすむ v2を追加掲載しておきました

AppleScript名:都道府県リストから隣接都道府県を含む該当のコードを抽出する
— Created 2015-12-06 by Takaaki Naganoya
— 2015 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use BridgePlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html

property prefList : {{prefCode:1, prefName:"北海道", neighbors:{}}, {prefCode:2, prefName:"青森県", neighbors:{3, 5}}, {prefCode:3, prefName:"岩手県", neighbors:{2, 4, 5}}, {prefCode:4, prefName:"宮城県", neighbors:{3, 5, 6, 7}}, {prefCode:5, prefName:"秋田県", neighbors:{2, 3, 4, 6}}, {prefCode:6, prefName:"山形県", neighbors:{3, 4, 5, 7, 15}}, {prefCode:7, prefName:"福島県", neighbors:{4, 6, 8, 9, 10, 15}}, {prefCode:8, prefName:"茨城県", neighbors:{7, 9, 10, 11, 12}}, {prefCode:9, prefName:"栃木県", neighbors:{7, 8, 10, 11, 12}}, {prefCode:10, prefName:"群馬県", neighbors:{7, 9, 11, 15, 20}}, {prefCode:11, prefName:"埼玉県", neighbors:{8, 9, 10, 12, 13, 19, 20}}, {prefCode:12, prefName:"千葉県", neighbors:{8, 11, 13}}, {prefCode:13, prefName:"東京都", neighbors:{11, 12, 19, 14}}, {prefCode:14, prefName:"神奈川県", neighbors:{13, 19, 22}}, {prefCode:15, prefName:"新潟県", neighbors:{6, 7, 10, 16, 20}}, {prefCode:16, prefName:"富山県", neighbors:{15, 17, 20, 21}}, {prefCode:17, prefName:"石川県", neighbors:{16, 18, 21}}, {prefCode:18, prefName:"福井県", neighbors:{17, 21, 25, 26}}, {prefCode:19, prefName:"山梨県", neighbors:{11, 13, 14, 20, 22}}, {prefCode:20, prefName:"長野県", neighbors:{10, 11, 15, 16, 19, 21, 22, 23}}, {prefCode:21, prefName:"岐阜県", neighbors:{16, 17, 18, 20, 23, 24, 25}}, {prefCode:22, prefName:"静岡県", neighbors:{14, 19, 20, 23}}, {prefCode:23, prefName:"愛知県", neighbors:{20, 21, 22, 24}}, {prefCode:24, prefName:"三重県", neighbors:{21, 23, 25, 26, 29}}, {prefCode:25, prefName:"滋賀県", neighbors:{18, 21, 24, 26}}, {prefCode:26, prefName:"京都府", neighbors:{18, 24, 25, 27, 28, 29}}, {prefCode:27, prefName:"大阪府", neighbors:{26, 29, 28, 30}}, {prefCode:28, prefName:"兵庫県", neighbors:{26, 27, 31, 33}}, {prefCode:29, prefName:"奈良県", neighbors:{24, 25, 26, 27, 30}}, {prefCode:30, prefName:"和歌山県", neighbors:{24, 27, 29}}, {prefCode:31, prefName:"鳥取県", neighbors:{28, 33, 32, 34}}, {prefCode:32, prefName:"島根県", neighbors:{31, 34, 35}}, {prefCode:33, prefName:"岡山県", neighbors:{28, 31, 34}}, {prefCode:34, prefName:"広島県", neighbors:{33, 31, 32, 35}}, {prefCode:35, prefName:"山口県", neighbors:{32, 34}}, {prefCode:36, prefName:"徳島県", neighbors:{37, 39}}, {prefCode:37, prefName:"香川県", neighbors:{36, 38, 39}}, {prefCode:38, prefName:"愛媛県", neighbors:{37, 39}}, {prefCode:39, prefName:"高知県", neighbors:{36, 37, 38}}, {prefCode:40, prefName:"福岡県", neighbors:{44, 43, 41}}, {prefCode:41, prefName:"佐賀県", neighbors:{40, 42}}, {prefCode:42, prefName:"長崎県", neighbors:{41}}, {prefCode:43, prefName:"熊本県", neighbors:{40, 42, 44, 45, 46}}, {prefCode:44, prefName:"大分県", neighbors:{40, 43, 45}}, {prefCode:45, prefName:"宮崎県", neighbors:{43, 44, 46}}, {prefCode:46, prefName:"鹿児島県", neighbors:{43, 45}}, {prefCode:47, prefName:"沖縄県", neighbors:{}}}

set aPref to 13 –Tokyo
set aRes to my filterRecListByLabel2(prefList, "prefCode == [c]%@", {aPref})
set targList to neighbors of aRes & aPref
–>  {​​​​​11, ​​​​​12, ​​​​​19, ​​​​​14, ​​​​​13​​​}–Saitama, Chiba, yamanashi, Kanagawa, Tokyo

–リストに入れたレコードを、指定の属性ラベルの値で抽出(predicateとパラメータを分離)し、1つのアイテムだけを返す
on filterRecListByLabel2(aRecList as list, aPredicate as string, aParam)
  set aArray to current application’s NSArray’s arrayWithArray:aRecList
  
set aPredicate to current application’s NSPredicate’s predicateWithFormat:aPredicate argumentArray:aParam
  
set filteredArray to aArray’s filteredArrayUsingPredicate:aPredicate
  
set bList to ASify from filteredArray as list
  
set cList to first item of bList
  
return cList
end filterRecListByLabel2

★Click Here to Open This Script 

AppleScript名:都道府県リストから隣接都道府県を含む該当のコードを抽出する v2.scpt
— Created 2015-12-06 by Takaaki Naganoya
— 2015 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
—BridgePlusを使わなくていいように書き換え

property prefList : {{prefCode:1, prefName:"北海道", neighbors:{}}, {prefCode:2, prefName:"青森県", neighbors:{3, 5}}, {prefCode:3, prefName:"岩手県", neighbors:{2, 4, 5}}, {prefCode:4, prefName:"宮城県", neighbors:{3, 5, 6, 7}}, {prefCode:5, prefName:"秋田県", neighbors:{2, 3, 4, 6}}, {prefCode:6, prefName:"山形県", neighbors:{3, 4, 5, 7, 15}}, {prefCode:7, prefName:"福島県", neighbors:{4, 6, 8, 9, 10, 15}}, {prefCode:8, prefName:"茨城県", neighbors:{7, 9, 10, 11, 12}}, {prefCode:9, prefName:"栃木県", neighbors:{7, 8, 10, 11, 12}}, {prefCode:10, prefName:"群馬県", neighbors:{7, 9, 11, 15, 20}}, {prefCode:11, prefName:"埼玉県", neighbors:{8, 9, 10, 12, 13, 19, 20}}, {prefCode:12, prefName:"千葉県", neighbors:{8, 11, 13}}, {prefCode:13, prefName:"東京都", neighbors:{11, 12, 19, 14}}, {prefCode:14, prefName:"神奈川県", neighbors:{13, 19, 22}}, {prefCode:15, prefName:"新潟県", neighbors:{6, 7, 10, 16, 20}}, {prefCode:16, prefName:"富山県", neighbors:{15, 17, 20, 21}}, {prefCode:17, prefName:"石川県", neighbors:{16, 18, 21}}, {prefCode:18, prefName:"福井県", neighbors:{17, 21, 25, 26}}, {prefCode:19, prefName:"山梨県", neighbors:{11, 13, 14, 20, 22}}, {prefCode:20, prefName:"長野県", neighbors:{10, 11, 15, 16, 19, 21, 22, 23}}, {prefCode:21, prefName:"岐阜県", neighbors:{16, 17, 18, 20, 23, 24, 25}}, {prefCode:22, prefName:"静岡県", neighbors:{14, 19, 20, 23}}, {prefCode:23, prefName:"愛知県", neighbors:{20, 21, 22, 24}}, {prefCode:24, prefName:"三重県", neighbors:{21, 23, 25, 26, 29}}, {prefCode:25, prefName:"滋賀県", neighbors:{18, 21, 24, 26}}, {prefCode:26, prefName:"京都府", neighbors:{18, 24, 25, 27, 28, 29}}, {prefCode:27, prefName:"大阪府", neighbors:{26, 29, 28, 30}}, {prefCode:28, prefName:"兵庫県", neighbors:{26, 27, 31, 33}}, {prefCode:29, prefName:"奈良県", neighbors:{24, 25, 26, 27, 30}}, {prefCode:30, prefName:"和歌山県", neighbors:{24, 27, 29}}, {prefCode:31, prefName:"鳥取県", neighbors:{28, 33, 32, 34}}, {prefCode:32, prefName:"島根県", neighbors:{31, 34, 35}}, {prefCode:33, prefName:"岡山県", neighbors:{28, 31, 34}}, {prefCode:34, prefName:"広島県", neighbors:{33, 31, 32, 35}}, {prefCode:35, prefName:"山口県", neighbors:{32, 34}}, {prefCode:36, prefName:"徳島県", neighbors:{37, 39}}, {prefCode:37, prefName:"香川県", neighbors:{36, 38, 39}}, {prefCode:38, prefName:"愛媛県", neighbors:{37, 39}}, {prefCode:39, prefName:"高知県", neighbors:{36, 37, 38}}, {prefCode:40, prefName:"福岡県", neighbors:{44, 43, 41}}, {prefCode:41, prefName:"佐賀県", neighbors:{40, 42}}, {prefCode:42, prefName:"長崎県", neighbors:{41}}, {prefCode:43, prefName:"熊本県", neighbors:{40, 42, 44, 45, 46}}, {prefCode:44, prefName:"大分県", neighbors:{40, 43, 45}}, {prefCode:45, prefName:"宮崎県", neighbors:{43, 44, 46}}, {prefCode:46, prefName:"鹿児島県", neighbors:{43, 45}}, {prefCode:47, prefName:"沖縄県", neighbors:{}}}

set aPref to 13 –Tokyo
set aRes to my filterRecListByLabel2(prefList, "prefCode == [c]%@", {aPref})
set targList to neighbors of aRes & aPref
–>  {​​​​​11, ​​​​​12, ​​​​​19, ​​​​​14, ​​​​​13​​​}–Saitama, Chiba, yamanashi, Kanagawa, Tokyo

–リストに入れたレコードを、指定の属性ラベルの値で抽出(predicateとパラメータを分離)し、1つのアイテムだけを返す
on filterRecListByLabel2(aRecList as list, aPredicate as string, aParam)
  set aArray to current application’s NSArray’s arrayWithArray:aRecList
  
set aPredicate to current application’s NSPredicate’s predicateWithFormat:aPredicate argumentArray:aParam
  
set filteredArray to aArray’s filteredArrayUsingPredicate:aPredicate
  
set bList to filteredArray as list
  
set cList to first item of bList
  
return cList
end filterRecListByLabel2

★Click Here to Open This Script 

Posted in geolocation list | Tagged 10.11savvy 10.12savvy 10.13savvy | 1 Comment

2点間の距離を求める v3

Posted on 2月 8, 2018 by Takaaki Naganoya

Core Locationを用いた地球上の2点間の距離計算のAppleScriptです。

地球は楕円体であるため、その楕円率を考慮して距離計算する必要があります。Core Locationには楕円率を考慮した計算が行われていることを期待しています。

完全球体として2点間の距離を計算すると近似値になってしまうものの、(東京近郊の2点の計算時)実際の計算結果を比較すると、4.2kmの距離計算時に1メートル程度の誤差としてしか表れてこないため、計算精度をさほど求められない用途においては、地球を完全球体として扱った近似計算でも十分に有用なケースがあります。

東京-鹿児島間で距離計算したときに、

  A. 楕円率を考慮して計算:960392.341424メートル
  B. 完全球体として近似計算:960581.2302612メートル
  誤差(A.- B.):189メートル

ぐらいの誤差です。日本国内の1点からの最寄りの施設を計算するぐらいなら完全球体として計算しても大丈夫でしょう。処理時間とか計算対象の座標数を考慮し、用途に応じて選択するとよいでしょう。

追記:
気になったので、CoreLocationで距離を求めるタイプ(First)と、完全球体で近似計算を行うタイプ(Second)のルーチンで1万回ループで実行して、実行時間を求めてみました。

MacBookPro10,1,  macOS Version 10.14.6 (Build 18G6006),  10000 iterations
         First Run   Total Time    Average  
First        0.005       11.614      0.001
Second       0.001        2.958      0.000
Ratio (excluding first run): 3.93:1

結果は、予想どおりCoreLocationで行うルーチン(本Script)の方が約4倍低速でした。ただし、実際の処理に組み込んでこの通りにならないケースもあるので、実際の処理で計測してどちらを用いるべきかを判断することになるでしょう。

AppleScript名:2点間の距離を求める v3
— Created 2015-03-03 by Shane Stanley
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "CoreLocation"

set aPlace to current application’s CLLocation’s alloc()’s initWithLatitude:35.737072 longitude:139.637826
set bPlace to current application’s CLLocation’s alloc()’s initWithLatitude:35.753108 longitude:139.595123
set distanceInMetres to aPlace’s distanceFromLocation:bPlace
–>  4252.71123319014

★Click Here to Open This Script 

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

(GET)Yahoo! 住所ジオコーダAPIを呼び出す

Posted on 2月 8, 2018 by Takaaki Naganoya

Yahoo!の住所ジオコーダAPIを呼び出して、住所情報から緯度、経度を取得するAppleScriptです。

実行のためには、Yahoo!に利用登録を行い、API Keyを取得してretAccessKey()ハンドラ内に書いておいてください。これは、サンプル用にわかりやすさを優先したためで、実際にはKeychainにAPI Keyを入れておいて問い合わせ、Script中にAPI Keyを直接書かないのが望ましいところです。

GoogleやYahoo!のWeb APIベース、AppleのOS内蔵住所ジオコーダなどさまざまですが、個人的にはYahoo!の住所ジオコーダをよく使っています。ただし、指定の住所がかならずしも一発で緯度・経度情報に変換できるわけでもないので、エラー時には後ろから1文字ずつ削除して再試行しています。

AppleScript名:(GET)Yahoo! 住所ジオコーダAPIを呼び出す
— Created 2016-11-25 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
–http://developer.yahoo.co.jp/webapi/map/openlocalplatform/v1/geocoder.html

set anAddress to "東京都墨田区押上1-1−2"
set aResList to addressGeoCoderByYahoo(anAddress) of me
–>  {​​​​​"35.66042757", ​​​​​"139.72918139"​​​}

on addressGeoCoderByYahoo(addrStr)
  set reqURLStr to "http://geo.search.olp.yahooapis.jp/OpenLocalPlatform/V1/geoCoder"
  
set aKey to retAccessKey() of me
  
set aRec to {query:addrStr, output:"json", appid:aKey, datum:"tky"} –日本測地系
  
set aURL to retURLwithParams(reqURLStr, aRec) of me
  
set aRes to callRestGETAPIAndParseResults(aURL) of me
  
  
set aRESCode to responseCode of aRes
  
if aRESCode is not equal to 200 then return false
  
set aRESHeader to responseHeader of aRes
  
  
set aJSONres to (json of aRes)
  
  
set anAdd1 to (aJSONres’s valueForKeyPath:"Feature")’s firstObject()
  
set aGPSstr to (anAdd1’s valueForKeyPath:"Geometry.Coordinates")
  
set {aLon, aLat} to separateStrByAMark(aGPSstr, ",") of me
  
  
return {aLat, aLon}
end addressGeoCoderByYahoo

–GET methodのREST APIを呼ぶ
on callRestGETAPIAndParseResults(aURL)
  set aRequest to current application’s NSMutableURLRequest’s requestWithURL:(current application’s |NSURL|’s URLWithString:aURL)
  
  
aRequest’s setHTTPMethod:"GET"
  
aRequest’s setCachePolicy:(current application’s NSURLRequestReloadIgnoringLocalCacheData)
  
aRequest’s setHTTPShouldHandleCookies:false
  
aRequest’s setTimeoutInterval:60
  
aRequest’s setValue:"application/json" forHTTPHeaderField:"Accept"
  
  
–CALL REST API
  
set aRes to current application’s NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value)
  
  
–Parse Results
  
set resList to aRes as list
  
  
set bRes to contents of (first item of resList)
  
set resStr to current application’s NSString’s alloc()’s initWithData:bRes encoding:(current application’s NSUTF8StringEncoding)
  
  
set jsonString to current application’s NSString’s stringWithString:resStr
  
set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
  
set aJsonDict to current application’s NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)
  
  
–Get Response Code & Header
  
set dRes to contents of second item of resList
  
if dRes is not equal to missing value then
    set resCode to (dRes’s statusCode()) as number
    
set resHeaders to (dRes’s allHeaderFields()) as record
  else
    set resCode to 0
    
set resHeaders to {}
  end if
  
  
return {json:aJsonDict, responseCode:resCode, responseHeader:resHeaders}
end callRestGETAPIAndParseResults

on retURLwithParams(aBaseURL, aRec)
  set aDic to current application’s NSMutableDictionary’s dictionaryWithDictionary:aRec
  
  
set aKeyList to (aDic’s allKeys()) as list
  
set aValList to (aDic’s allValues()) as list
  
set aLen to length of aKeyList
  
  
set qList to {}
  
repeat with i from 1 to aLen
    set aName to contents of item i of aKeyList
    
set aVal to contents of item i of aValList
    
set the end of qList to (current application’s NSURLQueryItem’s queryItemWithName:aName value:aVal)
  end repeat
  
  
set aComp to current application’s NSURLComponents’s alloc()’s initWithString:aBaseURL
  
aComp’s setQueryItems:qList
  
set aURL to (aComp’s |URL|()’s absoluteString()) as text
  
  
return aURL
end retURLwithParams

on retAccessKey()
  return "xxXxxxXxXXxxxXXXXXXXXXXXXXXxXXXxxxXXxXXxxXXxxxXXXxxXXXX-" –Yahoo! API Key
end retAccessKey

on urlencodeStr(aStr)
  set aString to current application’s NSString’s stringWithString:aStr
  
set aString to (aString’s stringByAddingPercentEncodingWithAllowedCharacters:(current application’s NSCharacterSet’s URLQueryAllowedCharacterSet())) as text
  
return aString
end urlencodeStr

–リストに入れたレコードを、指定の属性ラベルの値で抽出
on filterRecListByLabel1(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 filterRecListByLabel1

–テキストを指定記号を元に分割する
on separateStrByAMark(aStr as string, aMark as string)
  set aMarkLen to length of aMark
  
set aOffset to offset of "," in aStr
  
set aPart to text 1 thru (aOffset – 1) of aStr
  
set bPart to text (aOffset + aMarkLen) thru -1 of aStr
  
return {aPart, bPart}
end separateStrByAMark

★Click Here to Open This Script 

Posted in geolocation Internet REST API | Tagged 10.11savvy 10.12savvy 10.13savvy | 2 Comments

(GET)Yahoo! 逆住所ジオコーダAPIを呼び出す

Posted on 2月 8, 2018 by Takaaki Naganoya

Yahoo!の逆住所ジオコーダAPIを呼び出して、緯度、経度から住所情報を取得するAppleScriptです。

実行のためには、Yahoo!に利用登録を行い、API Keyを取得してretAccessKey()ハンドラ内に書いておいてください。これは、サンプル用にわかりやすさを優先したためで、実際にはKeychainにAPI Keyを入れておいて問い合わせ、Script中にAPI Keyを直接書かないのが望ましいところです。

AppleScript名:(GET)Yahoo! 逆住所ジオコーダAPIを呼び出す
— Created 2016-11-20 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"

–http://developer.yahoo.co.jp/webapi/map/openlocalplatform/v1/reversegeocoder.html

set aLat to 35.74 as string
set aLon to 139.6 as string
set aResList to reverseGeoCoderByYahoo(aLat, aLon) of me
–>  {​​​​​"東京都", ​​​​​"練馬区"​​​}

on reverseGeoCoderByYahoo(aLat, aLon)
  set reqURLStr to "http://reverse.search.olp.yahooapis.jp/OpenLocalPlatform/V1/reverseGeoCoder"
  
set aKey to retAccessKey() of me
  
set aRec to {lat:aLat, lon:aLon, output:"json", appid:aKey, datum:"tky"}
  
set aURL to retURLwithParams(reqURLStr, aRec) of me
  
set aRes to callRestGETAPIAndParseResults(aURL) of me
  
  
set aRESCode to responseCode of aRes
  
if aRESCode is not equal to 200 then return false
  
set aRESHeader to responseHeader of aRes
  
  
set aJSONres to (json of aRes)
  
set anAddress to (aJSONres’s valueForKeyPath:"Feature.Property")’s firstObject()
  
set anElement to anAddress’s valueForKeyPath:"AddressElement"
  
  
set aPrefecture to first item of (my filterRecListByLabel1(anElement, "Level == ’prefecture’"))
  
–>  {​​​​​Kana:"とうきょうと", ​​​​​Name:"東京都", ​​​​​Level:"prefecture", ​​​​​Code:"13"​​​}
  
  
set aCity to first item of (my filterRecListByLabel1(anElement, "Level == ’city’"))
  
–>  {​​​​​Kana:"ねりまく", ​​​​​Name:"練馬区", ​​​​​Level:"city", ​​​​​Code:"13120"​​​}
  
  
set aPref to |Name| of aPrefecture
  
set aCt to |Name| of aCity
  
return {aPref, aCt}
end reverseGeoCoderByYahoo

–GET methodのREST APIを呼ぶ
on callRestGETAPIAndParseResults(aURL)
  set aRequest to current application’s NSMutableURLRequest’s requestWithURL:(current application’s |NSURL|’s URLWithString:aURL)
  
  
aRequest’s setHTTPMethod:"GET"
  
aRequest’s setCachePolicy:(current application’s NSURLRequestReloadIgnoringLocalCacheData)
  
aRequest’s setHTTPShouldHandleCookies:false
  
aRequest’s setTimeoutInterval:60
  
aRequest’s setValue:"application/json" forHTTPHeaderField:"Accept"
  
  
–CALL REST API
  
set aRes to current application’s NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value)
  
  
–Parse Results
  
set resList to aRes as list
  
  
set bRes to contents of (first item of resList)
  
set resStr to current application’s NSString’s alloc()’s initWithData:bRes encoding:(current application’s NSUTF8StringEncoding)
  
  
set jsonString to current application’s NSString’s stringWithString:resStr
  
set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
  
set aJsonDict to current application’s NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)
  
  
–Get Response Code & Header
  
set dRes to contents of second item of resList
  
if dRes is not equal to missing value then
    set resCode to (dRes’s statusCode()) as number
    
set resHeaders to (dRes’s allHeaderFields()) as record
  else
    set resCode to 0
    
set resHeaders to {}
  end if
  
  
return {json:aJsonDict, responseCode:resCode, responseHeader:resHeaders}
end callRestGETAPIAndParseResults

on retURLwithParams(aBaseURL, aRec)
  set aDic to current application’s NSMutableDictionary’s dictionaryWithDictionary:aRec
  
  
set aKeyList to (aDic’s allKeys()) as list
  
set aValList to (aDic’s allValues()) as list
  
set aLen to length of aKeyList
  
  
set qList to {}
  
repeat with i from 1 to aLen
    set aName to contents of item i of aKeyList
    
set aVal to contents of item i of aValList
    
set the end of qList to (current application’s NSURLQueryItem’s queryItemWithName:aName value:aVal)
  end repeat
  
  
set aComp to current application’s NSURLComponents’s alloc()’s initWithString:aBaseURL
  
aComp’s setQueryItems:qList
  
set aURL to (aComp’s |URL|()’s absoluteString()) as text
  
  
return aURL
end retURLwithParams

on retAccessKey()
  return "xxXxxxXxXXxxxXXXXXXXXXXXXXXxXXXxxxXXxXXxxXXxxxXXXxxXXXX-" –Yahoo! API Key
end retAccessKey

on urlencodeStr(aStr)
  set aString to current application’s NSString’s stringWithString:aStr
  
set aString to (aString’s stringByAddingPercentEncodingWithAllowedCharacters:(current application’s NSCharacterSet’s URLQueryAllowedCharacterSet())) as text
  
return aString
end urlencodeStr

–リストに入れたレコードを、指定の属性ラベルの値で抽出
on filterRecListByLabel1(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 filterRecListByLabel1

★Click Here to Open This Script 

Posted in geolocation Internet REST API | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

Notification

Posted on 2月 8, 2018 by Takaaki Naganoya

macOS 10.10より、標準のScripting環境(スクリプトエディタ)上でCocoa-bridgeが使えるようになったので、Cocoa系のevent(Notification)が使えるようになったことを利用したサンプルです。

AppleScriptは基本的に、アプリケーションの機能や設定値を呼び出すためのもので、ごく一部の限られたアプリケーション以外からは「アプリケーション側からAppleScript(の所定のハンドラ)を呼び出す」方向のイベント呼び出しは利用できませんでした。

しかし、Cocoaのnotificationが利用できるようになったことで、多彩なイベント受信が行えるようになりました。

iTunes(or Music.app)の再生曲変更のnotificationを受信してやたらとTwitterなどに流そうとする入門Scripterがいるのですが(どこかの伝統芸か何かなんだろうか?)、そこはそんなに楽しいとも思えないし、本来のパワーを発揮していないとは思うのですが、見かけたらそっと触れないようにしておくことにしています。

AppleScript名:Notification
— Created 2017-10-13 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

property NSDistributedNotificationCenter : a reference to current application’s NSDistributedNotificationCenter

on run
  NSDistributedNotificationCenter’s defaultCenter()’s addObserver:me selector:"statusChanged:" |name|:"com.apple.iTunes.playerInfo" object:(missing value)
  
end run

on statusChanged:sender
  tell application id "com.apple.iTunes"
    try
      tell current track
        set anAlbum to album
        
set aName to name
        
set anArtist to album artist
        
set aRating to rating
      end tell
    on error
      return
    end try
  end tell
  
display notification aName
end statusChanged:

★Click Here to Open This Script 

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

指定Bundle IDのプロセス存在確認(ASOC)v3

Posted on 2月 8, 2018 by Takaaki Naganoya
AppleScript名:指定Bundle IDのプロセス存在確認(ASOC)v3
— Created 2015-02-08 by Takaaki Naganoya
— Created 2017-02-26 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

set aRes to chkAppProcesByBundleID("com.apple.iWork.Keynote")
–> true

on chkAppProcesByBundleID(aBundleID as string)
  set procArray to current application’s NSWorkspace’s sharedWorkspace’s runningApplications()’s valueForKeyPath:"bundleIdentifier"
  
return (procArray’s containsObject:aBundleID) as boolean
end chkAppProcesByBundleID

★Click Here to Open This Script 

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

バンドルIDで指定したプロセスを強制終了(NSRunningApplication)

Posted on 2月 8, 2018 by Takaaki Naganoya

バンドルIDで指定したプロセスを強制終了させるAppleScriptです。

Finderを終了させてみると、あえて意図して起動しないとFinderが終了したままの状態で戻って来ません。

AppleScript名:バンドルIDで指定したプロセスを強制終了(NSRunningApplication)
— Created 2017-09-17 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

set pRes to forceQuitAProcessByBUndleID("com.apple.finder") of me

–指定プロセスの強制終了
on forceQuitAProcessByBUndleID(aBundleID)
  set appArray to current application’s NSRunningApplication’s runningApplicationsWithBundleIdentifier:aBundleID
  
if appArray’s |count|() > 0 then
    set appItem to appArray’s objectAtIndex:0
    
set aRes to (appItem’s terminate()) as boolean
    
return aRes
  else
    return false
  end if
end forceQuitAProcessByBUndleID

★Click Here to Open This Script 

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

他のアプリケーションを隠す

Posted on 2月 8, 2018 by Takaaki Naganoya
AppleScript名:他のアプリケーションを隠す
— Created 2017-01-07 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

current application’s NSWorkspace’s sharedWorkspace()’s hideOtherApplications()

★Click Here to Open This Script 

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

指定名称のアプリケーションプロセスが存在すればその正しい名前を返す

Posted on 2月 8, 2018 by Takaaki Naganoya
AppleScript名:指定名称のアプリケーションプロセスが存在すればその正しい名前を返す
— Created 2015-07-29 16:43:11 +0900 by Takaaki Naganoya
— 2015 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aName to "メール"
set aRes to returnExactNameOfAnApp(aName) of me

on returnExactNameOfAnApp(aName)
  tell application "System Events"
    set ap1List to every process whose name is equal to aName
    
if ap1List = {} then
      set ap1List to every process whose displayed name is equal to aName
      
if ap1List = {} then return false
    end if
    
set anApp to contents of first item of ap1List
    
return name of anApp
  end tell
end returnExactNameOfAnApp

★Click Here to Open This Script 

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

ASOCで現在実行中のプロセスの情報を取得

Posted on 2月 8, 2018 by Takaaki Naganoya
AppleScript名:ASOCで現在実行中のプロセスの情報を取得
— Created 2015-09-08 by Takaaki Naganoya
— 2015 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set procInfo to current application’s NSProcessInfo’s processInfo()
–>  (NSProcessInfo) <NSProcessInfo: 0x6000000587e0>

set argList to procInfo’s arguments()
–>  (NSArray) {​​​​​"/Applications/ASObjC Explorer 4.app/Contents/MacOS/ASObjC Explorer 4"​​​}–ASObjC Explorer 4

set envList to procInfo’s environment()
–>  (NSDictionary) {​​​​​PATH:"/usr/bin:/bin:/usr/sbin:/sbin", ​​​​​TMPDIR:"/var/folders/h4/jfhlwst88xl9z0001s7k9vk00000gr/T/", ​​​​​LOGNAME:"me", ​​​​​HOME:"/Users/me", ​​​​​XPC_FLAGS:"0x0", ​​​​​Apple_PubSub_Socket_Render:"/private/tmp/com.apple.launchd.OLjjErklL4/Render", ​​​​​USER:"me", ​​​​​SSH_AUTH_SOCK:"/private/tmp/com.apple.launchd.y3g31V3gh7/Listeners", ​​​​​SECURITYSESSIONID:"XXXXX", ​​​​​DISPLAY:"/private/tmp/com.apple.launchd.63oTa9LGKM/org.macosforge.xquartz:0", ​​​​​XPC_SERVICE_NAME:"au.com.myriad-com.ASObjC-Explorer-4.69328", ​​​​​SHELL:"/bin/xxxx", ​​​​​__CF_USER_TEXT_ENCODING:"0x1F8:0x1:0xE"​​​}

set anUniqueStr to procInfo’s globallyUniqueString()
–>  (NSString) "XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX-XXXX-XXXXXXXXXXXXXXXX"

set anID to procInfo’s processIdentifier()
–>  4283–Unix Process ID (pid)

set aName to procInfo’s processName()
–>  (NSString) "ASObjC Explorer 4"

set aHostName to procInfo’s hostName()
–>  (NSString) "mbpretina.local"

set aVersionStr to procInfo’s operatingSystemVersionString()
–>  (NSString) "バージョン 10.10.5(ビルド 14F27)"

set aVersion to procInfo’s operatingSystemVersion()
–> can’t bridge argument of type {_NSOperatingSystemVersion=qqq}. OS X 10.10ではブリッジ不可。10.11でOK

set aCPUCores to procInfo’s processorCount()
–>  8

set activeCPUCores to procInfo’s activeProcessorCount()
–>  8

set anRAMcapacity to procInfo’s physicalMemory()
–>  8.589934592E+9

set anRAMcapacity to procInfo’s systemUptime()
–>  6.8344782485801E+4

set aThermalState to procInfo’s thermalState()
–>  0 –NSProcessInfoThermalStateNominal
(*
enum {
NSProcessInfoThermalStateNominal,
NSProcessInfoThermalStateFair,
NSProcessInfoThermalStateSerious,
NSProcessInfoThermalStateCritical
};
*)

★Click Here to Open This Script 

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

ASOCでプロセス情報を取得

Posted on 2月 8, 2018 by Takaaki Naganoya
AppleScript名:ASOCでプロセス情報を取得
— Created 2015-10-23 by Takaaki Naganoya
— 2015 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

set runningApplications to (current application’s NSWorkspace’s sharedWorkspace()’s runningApplications()) as list

repeat with i in runningApplications
  
  
set aName to (i’s localizedName()) as text
  
set anIcon to (i’s icon())
  
set anBundleID to (i’s bundleIdentifier()) as text
  
set anBundleURL to (i’s bundleURL())
  
  
set tmpArch to (i’s executableArchitecture())
  
if tmpArch = 16777223 then
    set anArch to "X86_64"
  else if tmpArch = 7 then
    set anArch to "I386 "
  else
    set anArch to "Another Arch (PPC? or Error)"
  end if
  
  
set anLaunchDate to (i’s launchDate())
  
set anFinishLaunch to (i’s isFinishedLaunching())
  
set aProcID to (i’s processIdentifier())
  
set anOwnMenubar to (i’s ownsMenuBar())
  
  
log {aName, tmpArch, anArch}
  
–> (* {"CCLibrary", -1, "Another Arch (PPC?)"} *)
end repeat

★Click Here to Open This Script 

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

NSRunningApplicationでアプリケーションプロセス情報を取得

Posted on 2月 8, 2018 by Takaaki Naganoya

AppleScriptで他のアプリケーションプロセスの情報を取得するには、OS標準装備のSystem Eventsに対して、

tell application "System Events"
	set aProp to properties of process "Safari"
end tell
--> {has scripting terminology:true, bundle identifier:"com.apple.Safari", file:alias "Macintosh HD:Applications:Safari.app:" of application "System Events", creator type:"sfri", subrole:missing value, entire contents:{}, selected:missing value, application file:alias "Cherry:Applications:Safari.app:" of application "System Events", orientation:missing value, role:"AXApplication", accepts high level events:true, file type:"APPL", value:missing value, position:missing value, id:909534, displayed name:"Safari", name:"Safari", class:application process, background only:false, frontmost:false, size:missing value, visible:true, Classic:false, role description:"application", maximum value:missing value, architecture:"x86_64", partition space used:0, short name:"Safari", focused:missing value, minimum value:missing value, help:missing value, title:"Safari", accepts remote events:false, total partition size:0, description:"application", accessibility description:missing value, enabled:missing value, unix id:3386}

などと操作することになります。ただし、System Eventsがつねに使えるわけではありません。

Mac App Storeに出すアプリケーションの中だと、些細な用途に他のアプリケーションを呼び出そうとしても、よほどの理由がないかぎり通りません(リジェクトされます)。Dark Mode/Light Modeの検出に安直にSystem Eventsを使おうとしてリジェクトされた経験があります。

そこで、他のサービス(shell commandとか、Cocoa Frameworkとか)を経由して機能を呼び出すことになります。まったく同じことができるわけではありませんが、1つの目的に対して複数の方法を用意しておくのはセオリーです。OS側でバグを作られた場合の回避策とか(正しくないOSバージョンを返してくるmacOSがありました)、目的に応じて利用に制限がかかる用途(まさにMac App Storeがそれです)があるためです。

AppleScript名:NSRunningApplicationでアプリケーションプロセス情報を取得
— Created 2017-09-17 15:27:23 +0900 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

set aBundleID to "com.apple.Safari"

set appArray to current application’s NSRunningApplication’s runningApplicationsWithBundleIdentifier:aBundleID
if appArray’s |count|() = 0 then its return

set appItem to appArray’s objectAtIndex:0

set iconRes to (appItem’s icon())
(*
–>  (NSImage) <NSImage 0x618001478f40 Size={32, 32} Reps=(
"<NSIconRefImageRep:0x61800029b300 iconRef=0x8403 size:128×128 pixels:128×128>",
"<NSIconRefImageRep:0x61800089f7c0 iconRef=0x8403 size:128×128 pixels:256×256>",
"<NSIconRefImageRep:0x6180006989c0 iconRef=0x8403 size:256×256 pixels:256×256>",
"<NSIconRefImageRep:0x61800129f040 iconRef=0x8403 size:256×256 pixels:512×512>",
"<NSIconRefImageRep:0x618000898dd0 iconRef=0x8403 size:512×512 pixels:512×512>",
"<NSIconRefImageRep:0x61800029a270 iconRef=0x8403 size:48×48 pixels:48×48>",
"<NSIconRefImageRep:0x618000691490 iconRef=0x8403 size:36×36 pixels:36×36>",
"<NSIconRefImageRep:0x618000c8e100 iconRef=0x8403 size:36×36 pixels:72×72>",
"<NSIconRefImageRep:0x618000a80460 iconRef=0x8403 size:32×32 pixels:32×32>",
"<NSIconRefImageRep:0x6180004899c0 iconRef=0x8403 size:32×32 pixels:64×64>",
"<NSIconRefImageRep:0x61800089db50 iconRef=0x8403 size:18×18 pixels:18×18>",
"<NSIconRefImageRep:0x618001291530 iconRef=0x8403 size:18×18 pixels:36×36>",
"<NSIconRefImageRep:0x618000c8e0b0 iconRef=0x8403 size:16×16 pixels:16×16>",
"<NSIconRefImageRep:0x618000880280 iconRef=0x8403 size:16×16 pixels:32×32>",
"<NSIconRefImageRep:0x618000e99a50 iconRef=0x8403 size:512×512 pixels:1024×1024>"
)>
*)


set locRes to (appItem’s localizedName()) as string
–>  "Safari"

set bID to (appItem’s bundleIdentifier()) as string
–>  "com.apple.Safari"

set bURL to (appItem’s bundleURL()) as string
–>  "Cherry:Applications:Safari.app:"

set arch to (appItem’s executableArchitecture())
–>  16777223

set exeURL to (appItem’s executableURL())
–>  (NSURL) file:///Applications/Safari.app/Contents/MacOS/Safari

set launchDate to (appItem’s launchDate())
–>  (NSDate) 2017-09-13 01:58:16 +0000

set launchFinish to (appItem’s finishedLaunching) as integer
–>  1

set pID to (appItem’s processIdentifier) as integer
–>  11877

set oenMenu to (appItem’s ownsMenuBar()) as boolean
–>  false

★Click Here to Open This Script 

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

アプレットのアイコンをDockに出さない2

Posted on 2月 8, 2018 by Takaaki Naganoya
AppleScript名:アプレットのアイコンをDockに出さない2
— Created 2015-10-22 by Takaaki Naganoya
— 2015 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

–Dockアイコン非表示、dialogも出ない
current application’s NSApp’s setActivationPolicy:(current application’s NSApplicationActivationPolicyProhibited)
repeat with i from 1 to 10
  tell current application
    display notification (i as text)
    
delay 1
  end tell
end repeat
quit

★Click Here to Open This Script 

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

Dockとメニューバーを隠す→戻す

Posted on 2月 8, 2018 by Takaaki Naganoya
AppleScript名:Dockとメニューバーを隠す→戻す
— Created 2017-03-15 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

–Main MenuとDockを隠す
current application’s NSApplication’s sharedApplication()’s setPresentationOptions:10 –NSApplicationPresentationHideMenuBar | NSApplicationPresentationHideDock

delay 10

–MenuとDockを通常に戻す
current application’s NSApplication’s sharedApplication()’s setPresentationOptions:(current application’s NSApplicationPresentationDefault)

★Click Here to Open This Script 

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

Dockアイコンにプログレスバーを追加

Posted on 2月 8, 2018 by Takaaki Naganoya

Dockアイコンにプレグレスバーを描画するAppleScriptです。

書き方のクセがあからさまに違うこの内容は、Edama2さんからいただいたものですね、コレ。

–> Demo Movie

AppleScript名:Dockアイコンにプログレスバーを追加
use AppleScript
use framework "Foundation"
use scripting additions

on run
  set max to 100
  
repeat with num from 1 to max
    my progDockTile(max, num)
    
delay 0.1
  end repeat
  
  
#アイコンを元に戻す
  
current application’s NSApp’s setApplicationIconImage:(current application’s NSImage’s imageNamed:"NSApplicationIcon")
end run

#Dockアイコンにプログレスバーを追加
on progDockTile(max, current)
  set appIcon to current application’s NSImage’s imageNamed:"NSApplicationIcon"
  
set iconSize to appIcon’s |size|()
  
  
tell (current application’s NSImage’s alloc()’s initWithSize:iconSize)
    
    
lockFocus()
    
    
appIcon’s dissolveToPoint:(current application’s NSZeroPoint) fraction:1.0
    
set n to (iconSize’s width) / 16
    
    
#プログレスバーの長方形
    
set myRect to current application’s NSMakeRect(n / 2, n, n * 15, n * 1.6) –>{origin:{x:4.0, y:8.0}, |size|:{width:120.0, height:12.800000190735}}
    
    
tell (current application’s NSBezierPath’s ¬
      bezierPathWithRoundedRect:myRect ¬
        xRadius:(myRect’s |size|’s height) / 2 ¬
        
yRadius:(myRect’s |size|’s height) / 2)
      
      
current application’s (NSColor’s colorWithWhite:1.0 alpha:0.4)’s |set|() –>背景色
      
fill()
      
      
current application’s NSColor’s whiteColor()’s |set|() –>枠色
      
stroke()
    end tell
    
    
if current is greater than 0 then
      
      
if current is greater than max then set current to max
      
      
set myRect’s |size|’s width to (myRect’s |size|’s width) / max * current
      
      
tell (current application’s NSBezierPath’s ¬
        bezierPathWithRoundedRect:myRect ¬
          xRadius:(myRect’s |size|’s height) / 2 ¬
          
yRadius:(myRect’s |size|’s height) / 2)
        
        
set strartColor to current application’s NSColor’s colorWithRed:0.15 green:0.55 blue:1 alpha:0.8
        
set endColor to strartColor’s shadowWithLevel:0.7
        
set grad to current application’s NSGradient’s alloc()’s initWithStartingColor:strartColor endingColor:endColor
        
grad’s drawInBezierPath:it angle:270.0
      end tell
    end if
    
    
unlockFocus()
    
    
current application’s NSApp’s setApplicationIconImage:it
  end tell
  
  
return (current + 1)
end progDockTile

★Click Here to Open This Script 

Posted in Icon Image System | Tagged 10.11savvy 10.12savvy 10.13savvy Dock | 1 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 (187) 13.0savvy (61) 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