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

カテゴリー: 未分類

ASOCでハンドラの間接呼び出し v3b

Posted on 3月 7, 2018 by Takaaki Naganoya

パラメータ付きのハンドラの存在確認を行い、間接的に呼び出しを行うAppleScriptです。

ハンドラの間接呼び出しについては、最近研究を重ねておりました。過去の事例をまとめ、ノウハウの方向性を分析。

過去の事例はPure AppleScriptについてのものであり、今日Cocoaの機能を呼び出すAppleScriptObjCの環境で使えないものを紹介しても仕方がありません。もはや、Cocoaの機能呼び出しとAppleScriptは不可分のレベルです。

そんなわけで、AppleScriptObjC環境でのハンドラの間接呼び出しについて試行錯誤と検討を重ね、ハンドラの存在確認とパラメータつきハンドラの間接呼び出しができるようになってきました。

AppleScript名:ASOCでハンドラの間接呼び出し v3b
–By @badcharanさん
use framework "Foundation"
use scripting additions

set nameOfTargetHandler to "aHandler:"
set aParamObj to {1, 2, 3}

set existsHandler to (me’s respondsToSelector:nameOfTargetHandler) as boolean
if existsHandler = true then
  set aRes to (my performSelector:nameOfTargetHandler withObject:aParamObj) as list of string or string
else
  error "The Handler \"" & nameOfTargetHandler & "\" is not present in this Script."
end if

return aRes

on x()
  return "hello"
end x

on aHandler:(aParam as list of string or string)
  return length of aParam
end aHandler:

★Click Here to Open This Script 

「as anything」がmacOS 10.13までは「as list ot list of string」などと解釈されていましたが、macOS 10.14からは「as anything」(Script Editor)あるいは「as any」(Script Debugger)と解釈されるようになりました。このあたり、足並み揃えてほしいところですが……

とりあえず、macOS 10.14移行の環境(Script Editor)で動く(正しく解釈される)ように少し書き換えてみました。

AppleScript名:ASOCでハンドラの間接呼び出し v3c.scpt
–By @badcharanさん
use framework "Foundation"
use scripting additions

set nameOfTargetHandler to "aHandler:"
set aParamObj to {1, 2, 3}

set existsHandler to (me’s respondsToSelector:nameOfTargetHandler) as boolean
if existsHandler = true then
  set aRes to (my performSelector:nameOfTargetHandler withObject:aParamObj) as anything –Script Editor, "any" for Script Debugger
else
  error "The Handler \"" & nameOfTargetHandler & "\" is not present in this Script."
end if

return aRes

on x()
  return "hello"
end x

on aHandler:(aParam as anything) –Script Editor, "any" for Script Debugger
  return length of aParam
end aHandler:

★Click Here to Open This Script 

Posted in 未分類 | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

Mail.appで送信元を明示的に指定しつつメール送信

Posted on 2月 26, 2018 by Takaaki Naganoya

Mail.appで送信元の氏名および送信先の氏名を明示的に指定しつつメール送信を行うAppleScriptです。

Mail.appの操作Scriptはやればやるだけ課題が見えてくるので、ほどほどのところで止めています。メールアカウントについて、1つしかアカウントを登録していなければデフォルトのメールアカウントでを前提にメール送信すればいいはずですが、複数のアカウントを登録しているケースが多いので、アカウントを指定する処理を書いてみました。

メールの大量連続送信については、1990年代からずっとAppleScriptで行なっていますが、途中で知り合いが盛大にやらかした(短時間で大量のメール送信を行なってさまざまなプロバイダのメールサーバーをコケさせかけた)影響もあって、日本国内のネットワークでは単位時間内に所定の本数を超えるメール送信を行うと、プロバイダからすぐにメール送信のリレーを止められ、その後しばらくメール送信を行えなくなるのが「お約束」となっています。

# だいたい、1秒に1通以上(1分に60通以上)送信すると疑われます。

かくして、メールの大量一括送信をScriptで行う場合には、ある程度「待ち時間」を入れつつ送信するか、REST API経由でSendGridのサービスを呼び出してメーラー以外でメール送信を行うあたりが落とし所になっています。

AppleScript名:Mail.appで送信元を明示的に指定しつつメール送信

–本来、メールアカウントのnameがわかっていれば、わざわざリストから選択する必要はない
tell application "Mail"
  set aList to name of every account
  
set aaSel to choose from list aList
  
if aaSel = {} or aaSel = false then return
  
set aSel to first item of aaSel
end tell

log aSel

set mRes to makeAMailWithSpecifiedAccount("メールのタイトルだよ100", "テストメールの本文だよ100", "送信者の名前だよ", "送付先氏名", "nobody@piyocast.com", aSel) of me

on makeAMailWithSpecifiedAccount(theSubject, theBody, myName, yourName, yourAddress, myAccountName)
  tell application "Mail"
    –Account check
    
set anAccount to account myAccountName
    
set aProp to enabled of anAccount
    
if aProp is not equal to true then return false –アカウントが有効でなかった場合はエラーに
    
    
set eList to (email addresses of anAccount)
    
if eList = {} then return false –アカウントにメールアドレスの登録がなかった場合はエラーに(ねんのため)
    
set myEmail to first item of eList
    
    
–Make Sender
    
set theSender to myName & "<" & myEmail & ">"
    
    
–Make Message & Send
    
set newMessage to make new outgoing message with properties {sender:theSender, subject:theSubject, content:theBody & return & return, visible:false}
    
tell newMessage
      set visible to false
      
make new to recipient at end of to recipients with properties {name:yourName, address:yourAddress} –受取人の指定
      
send –メール送信
    end tell
  end tell
  
  
return true
end makeAMailWithSpecifiedAccount

★Click Here to Open This Script 

Posted in 未分類 | Tagged 10.11savvy 10.12savvy 10.13savvy Mail | 1 Comment

Keynoteでタイトルがマスターに入っていなかった場合に修正 v2

Posted on 2月 23, 2018 by Takaaki Naganoya

Keynoteのタイトルがマスターに入っていなかった場合に、スライドの一番左上に存在するテキストアイテムをタイトルに入れ直すAppleScriptです。

Keynoteで急いでスライドを作ってはみたものの、あわててタイトルをマスタースライドのタイトルオブジェクトに入れていなかった場合に、(見た目は変わらないものの)あとで修正しておく必要があるケースがあります(Keynoteのデータを再利用するような場合とか)。

そこで、現在表示中のスライド中で一番左上に存在するテキストアイテムで、フォントサイズ20ポイント以上、文字色が黒 {0, 0, 0} のものをタイトルとみなして、文字を取得。あらためてマスタースライドのタイトルオブジェクトに文字を入れ直します。

このままだと、色データが{0,0,1}になったぐらいで検出できなくなってしまいますが、Color Domain処理を組み合わせることで「だいたい黒」「黒っぽい色」といった判定が行えるため、組み合わせて使うと効果的でしょう。


▲Before


▲After

AppleScript名:Keynoteでタイトルがマスターに入っていなかった場合に矯正 v2
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
use bPlus : script "BridgePlus"

property minTitleFontSize : 20
property targBaseSlide : "タイトル(上)"

tell application "Keynote"
  tell front document
    set newMaster to master slide targBaseSlide
    
    
tell current slide
      set curBaseName to name of base slide
      
      
–現在のスライド上のすべてのテキストアイテムの座標{x ,y}を取得して最小(一番上)のものを推測
      
set pList to position of every text item
      
set p2List to sort2DListAscending(pList) of me
      
set mostUpPos to contents of first item of p2List
      
      
–一番上に存在するテキストアイテムから情報を抽出
      
set tTextItem to first item of (every text item whose position is mostUpPos)
      
tell tTextItem
        set tObjText to (object text)
        
set aTitle to tObjText as string
        
set aProp to size of (object text)
        
set aColor to color of (object text)
      end tell
      
      
–スライドの一番上に存在するテキストアイテムのフォントサイズが想定以上で、色が黒の場合には
      
–マスタースライドのデフォルトタイトルに、一番上に存在していたテキストアイテムの本文を突っ込む
      
if (aProp ≥ minTitleFontSize) and (aColor = {0, 0, 0}) then
        –色判定でColor Domain処理を行えば、「黒っぽい色」や「赤っぽい色」を対象にできる
        
        
if curBaseName is not equal to targBaseSlide then
          set base slide to newMaster
        end if
        
        
delete tTextItem
        
set object text of default title item to aTitle
      end if
    end tell
  end tell
end tell

–2D Listのソート
on sort2DListAscending(aList)
  load framework
  
set sortIndexes to {0, 1} –Key Item id: begin from 0
  
set sortOrders to {true, true} –Ascending, Ascending
  
set sortTypes to {"compare:", "compare:"}
  
set bList to (current application’s SMSForder’s subarraysIn:(aList) sortedByIndexes:sortIndexes ascending:sortOrders sortTypes:sortTypes |error|:(missing value))
  
return bList as list of string or string
end sort2DListAscending

★Click Here to Open This Script 

Keynote Control 1

Keynote Control 2

Posted in 未分類 | Tagged 10.11savvy 10.12savvy 10.13savvy Keynote | Leave a comment

Finder Windowを円運動 v1b

Posted on 2月 23, 2018 by Takaaki Naganoya

Finderのウィンドウ1つを画面上で楕円運動させるAppleScriptです。

三角関数の計算にShane StanleyのBridge Plus AppleScript Librariesを利用しています。

以前に掲載したバージョン(v1)から、BridgePlusを使うように変更しました。

–> Demo Movie

自分が最初に見たAppleScriptが、「AppleScript道入門」に掲載されていたテキストエディタのウィンドウを画面の淵に沿わせて直線移動で1周させるものでした(それを見て「たいしたことはない」と思ってしまいましたが)。

いまでは、楕円軌道に沿ったウィンドウ移動もこのぐらいのスピードでできている、ということを実感できました。

AppleScript名:Finder Windowを円運動 v1b
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use bPlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html
use framework "AppKit" — for NSScreen

property aNum : 300
property aOffset : 0

load framework

set {newX, newY} to (current application’s NSScreen’s mainScreen()’s frame()’s |size|()) as list

tell application "Finder"
  set aWin to make new Finder window
end tell

repeat with i from 1 to 360 by 6
  
  
set |asin| to (current application’s SMSForder’s sinValueOf:i) as real
  
set |acos| to (current application’s SMSForder’s cosValueOf:i) as real
  
  
set x to ((aNum * |asin|) + aNum) * 2.0 + aOffset
  
set y to (aNum * |acos|) + aNum + aOffset
  
  
tell application "Finder"
    tell aWin
      set position to {x as integer, y as integer}
    end tell
  end tell
  
end repeat

tell application "Finder"
  tell aWin
    close
  end tell
end tell

★Click Here to Open This Script 

Posted in 未分類 | Tagged 10.11savvy 10.12savvy 10.13savvy Finder | Leave a comment

テーマを選択してKeynoteの新規ドキュメントを作成

Posted on 2月 23, 2018 by Takaaki Naganoya

AppleScript名:テーマを選択してKeynoteの新規ドキュメントを作成
tell application "Keynote"
  set tList to name of every theme
  
set themeName to first item of (choose from list tList)
  
set newDoc to make new document with properties {document theme:theme themeName}
  
end tell

★Click Here to Open This Script 

Keynote Control 1

Keynote Control 2

Posted in 未分類 | Tagged 10.11savvy 10.12savvy 10.13savvy Keynote | Leave a comment

Keynote 6.6でドキュメントを新規作成して指定可能なマスタースライド一覧を取得

Posted on 2月 23, 2018 by Takaaki Naganoya
AppleScript名:Keynote 6.6でドキュメントを新規作成して指定可能なマスタースライド一覧を取得
— Created 2015-10-27 by Takaaki Naganoya
— 2015 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

tell application "Keynote"
  –Keynoteの用語辞書のサンプルどおりに書いても動かない!!(オブジェクト名がローカライズされているので、テーマ名が言語環境依存)
  
set aDoc to (make new document with properties {document theme:theme "ホワイト", width:1920, height:1080})
  
  
tell aDoc
    set masList to name of every master slide
    
–> {"タイトル & サブタイトル", "画像(横長)", "タイトル(中央)", "画像(縦長)", "タイトル(上)", "タイトル & 箇条書き", "タイトル、箇条書き、画像", "箇条書き", "画像(3 点)", "引用", "写真", "空白"}
  end tell
end tell

★Click Here to Open This Script 

Keynote Control 1

Keynote Control 2

Posted in 未分類 | Tagged 10.11savvy 10.12savvy 10.13savvy Keynote | Leave a comment

Keynoteで現在表示中のスライド上にある表のカラム幅を自動調整

Posted on 2月 23, 2018 by Takaaki Naganoya


▲Before


▲After

AppleScript名:Keynoteで現在表示中のスライド上にある表のカラム幅を自動調整 v2
— Created 2017-10-06 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

tell application "Keynote"
  tell front document
    tell current slide
      
      
set tCount to count every table
      
if tCount = 0 then return
      
      
tell table 1
        
        
set cCount to count every column
        
set cWidth to width of every column
        
set aWidth to width –table width
        
        
set aveWidth to (aWidth – (first item of cWidth)) / (cCount – 1)
        
        
tell columns 2 thru cCount
          set width to aveWidth
        end tell
        
      end tell
    end tell
  end tell
end tell

★Click Here to Open This Script 

Keynote Control 1

Keynote Control 2

Posted in 未分類 | Tagged 10.11savvy 10.12savvy 10.13savvy Keynote | Leave a comment

Keynoteで2D円グラフを作成

Posted on 2月 23, 2018 by Takaaki Naganoya

Keynoteの現在の書類上に2D円グラフを作成するAppleScriptです。

その他、さまざまなKeynote上で作成可能なグラフを作成するAppleScriptについては、電子書籍「Keynote Control with AppleScript」❶❷で解説しています。

AppleScript名:Keynoteで2D円グラフを作成
–set aRec to {kanjiNum:24960, kanjiRating:28.0, hiraganaNum:56080, hiraganaRating:62.9, katakanaNum:1063, katakanaRating:1.2, otherNum:7136, otherRating:8.0} –文学(坊ちゃん)
–set aRec to {kanjiNum:563289, kanjiRating:22.0, hiraganaNum:1311933, hiraganaRating:51.2, katakanaNum:210161, katakanaRating:8.2, otherNum:478690, otherRating:18.7, totalCount:2564073}–異世界はスマートフォンとともに
set aRec to {kanjiNum:364870, kanjiRating:26.7, hiraganaNum:682261, hiraganaRating:50.0, katakanaNum:89109, katakanaRating:6.6, otherNum:230374, otherRating:16.9, totalCount:1366614} –Knight’s and Magic

set cCount to (kanjiNum of aRec)
set hCount to (hiraganaNum of aRec)
set kCount to (katakanaNum of aRec)
set oCount to (otherNum of aRec)

tell application "Keynote"
  set aDoc to (make new document with properties {document theme:theme "ホワイト", width:1024, height:768}) –Normal Slide
  
–set aDoc to (make new document with properties {document theme:theme "ホワイト", width:1920, height:1080})–Wide Slide
  
  
tell front document
    
    
set aSlide to make new slide at after (first slide) with properties {base slide:master slide "タイトル(上)"}
    
tell aSlide
      set object text of default title item to "文字種別使用比率"
      
–add chart row names {"文字種別"} column names {"漢字", "ひらがな", "カタカナ", "その他"} data {{cCount, hCount, kCount, oCount}} type pie_2d –group by chart row
      
add chart row names {"文字種別"} column names {"Kanji", "Hiragana", "Katakana", "Other"} data {{cCount, hCount, kCount, oCount}} type pie_2d –group by chart row
      
    end tell
    
  end tell
  
end tell

★Click Here to Open This Script 

Keynote Control 1

Keynote Control 2

Posted in 未分類 | Tagged 10.11savvy 10.12savvy 10.13savvy Keynote | Leave a comment

2つのパスの相対パスを求める v3

Posted on 2月 19, 2018 by Takaaki Naganoya
AppleScript名:2つのパスの相対パスを求める v3
— Created 2017-01-28 by Takaaki Naganoya
— Modified 2017-01-30 by Shane Stanley
— Modified 2017-02-03 by Takaaki Naganoya
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

–Markdown書類(フルパス)
set aFile to "/Users/me/Documents/ぴよまるソフトウェア/書籍原稿/–Book 1a「AppleScript最新リファレンス バージョン2.7対応」/5000 iOSデバイスとの連携/5100 iOSデバイスからMacに画面を出力するAirServer.md"

–リンク画像(フルパス)
set bFile to "/Users/me/Documents/ぴよまるソフトウェア/書籍原稿/–Book 1a「AppleScript最新リファレンス バージョン2.7対応」/9999_images/img-1.php.jpeg"

set relativePath to calcRelativePath(aFile, bFile) of me
–>  "../9999_images/img-1.php.jpeg"

on calcRelativePath(aPOSIXfile, bPOSIXfile)
  set aStr to current application’s NSString’s stringWithString:aPOSIXfile
  
set bStr to current application’s NSString’s stringWithString:bPOSIXfile
  
  
set aList to aStr’s pathComponents() as list
  
set bList to bStr’s pathComponents() as list
  
  
set aLen to length of aList
  
set bLen to length of bList
  
  
if aLen ≥ bLen then
    copy aLen to aMax
  else
    copy bLen to aMax
  end if
  
  
repeat with i from 1 to aMax
    set aTmp to contents of item i of aList
    
set bTmp to contents of item i of bList
    
    
if aTmp is not equal to bTmp then
      exit repeat
    end if
  end repeat
  
  
set bbList to items i thru -1 of bList
  
set aaItem to (length of aList) – i
  
  
set tmpStr to {}
  
repeat with ii from 1 to aaItem
    set the end of tmpStr to ".."
  end repeat
  
  
set allRes to current application’s NSString’s pathWithComponents:(tmpStr & bbList)
  
return allRes as text
end calcRelativePath

★Click Here to Open This Script 

Posted in File path 未分類 | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

指定EnumがどのFrameworkに所属しているか検索 v2

Posted on 2月 8, 2018 by Takaaki Naganoya
AppleScript名:指定EnumがどのFrameworkに所属しているか検索 v2
— 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 NSFileManager : a reference to current application’s NSFileManager
property NSString : a reference to current application’s NSString
property NSPredicate : a reference to current application’s NSPredicate
property NSMutableArray : a reference to current application’s NSMutableArray
property NSUTF8StringEncoding : a reference to current application’s NSUTF8StringEncoding

set a1Res to searchEnumFromHeaderFiles("NSUTF8StringEncoding") of me
–>  {​​​​​"DiscRecording.framework", ​​​​​"Foundation.framework", ​​​​​"SpriteKit.framework"​​​}

set a2Res to searchEnumFromHeaderFiles("NSNumberFormatterRoundUp") of me
–>  {​​​​​"Foundation.framework"​​​}

set a3Res to searchEnumFromHeaderFiles("NSParagraphStyleAttributeName") of me
–>  {​​​​​"AppKit.framework"​​​}

on searchEnumFromHeaderFiles(targString)
  set aClass to current application’s NSClassFromString(targString)
  
if aClass is not equal to missing value then return false
  
  
set dPath to POSIX path of (path to application id "com.apple.dt.Xcode")
  
set aFol to dPath & "Contents/Developer/Platforms/MacOSX.platform/" & "Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
  
  
set bList to retFullPathWithinAFolderWithRecursiveFilterByExt(aFol, "h") of me
  
set matchedList to {}
  
  
repeat with i in bList
    set j to contents of i
    
    
set aStr to (NSString’s stringWithContentsOfFile:j encoding:NSUTF8StringEncoding |error|:(missing value))
    
if aStr ≠ missing value then
      set aRange to (aStr’s rangeOfString:targString)
      
      
if aRange’s location() ≠ current application’s NSNotFound and (aRange’s location()) < 9.99999999E+8 then
        set tmpStr to (current application’s NSString’s stringWithString:j)
        
set pathList to tmpStr’s pathComponents()
        
set thePred to (current application’s NSPredicate’s predicateWithFormat:"pathExtension == ’framework’")
        
set aRes to (pathList’s filteredArrayUsingPredicate:thePred)’s firstObject() as text
        
set the end of matchedList to aRes
      end if
      
    end if
  end repeat
  
  
set aArray to current application’s NSArray’s arrayWithArray:matchedList
  
set bArray to aArray’s valueForKeyPath:"@distinctUnionOfObjects.self"
  
return bArray as list
end searchEnumFromHeaderFiles

–指定フォルダ以下のすべてのファイルを再帰で取得(拡張子で絞り込み)
on retFullPathWithinAFolderWithRecursiveFilterByExt(aFol, aExt)
  set anArray to NSMutableArray’s array()
  
set aPath to NSString’s stringWithString:aFol
  
set dirEnum to NSFileManager’s defaultManager()’s enumeratorAtPath:aPath
  
  
repeat
    set aName to (dirEnum’s nextObject())
    
if aName = missing value then exit repeat
    
set aFullPath to aPath’s stringByAppendingPathComponent:aName
    
anArray’s addObject:aFullPath
  end repeat
  
  
set thePred to NSPredicate’s predicateWithFormat:"pathExtension == [c]%@" argumentArray:{aExt}
  
set bArray to anArray’s filteredArrayUsingPredicate:thePred
  
  
return bArray as list
end retFullPathWithinAFolderWithRecursiveFilterByExt

★Click Here to Open This Script 

Posted in recursive call 未分類 | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

指定クラスがどのFrameworkに所属しているか検索 v3

Posted on 2月 8, 2018 by Takaaki Naganoya
AppleScript名:指定クラスがどのFrameworkに所属しているか検索 v3
— Created 2017-10-14 by Shane Stanley
— Modified 2017-10-14 by Takaaki Naganoya
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set fRes1 to searchClassInFrameworks("JSContext") of me
–>  "JavaScriptCore.framework"

set fRes2 to searchClassInFrameworks("NSApplication") of me
–>  "AppKit.framework"

set fRes3 to searchClassInFrameworks("NSRect") of me
–>  false

set fRes4 to searchClassInFrameworks("PDFPage") of me
–>  "Quartz.framework"

set fRes5 to searchClassInFrameworks("NSUTF8StringEncoding") of me
–>  false

set fRes6 to searchClassInFrameworks("CIColor") of me
–>  "CoreImage.framework"

on searchClassInFrameworks(aTarget)
  set aClass to current application’s NSClassFromString(aTarget)
  
if aClass = missing value then return false
  
set theComponenents to (current application’s NSBundle’s bundleForClass:aClass)’s bundleURL’s pathComponents()
  
set thePred to current application’s NSPredicate’s predicateWithFormat:"pathExtension == ’framework’"
  
set aRes to (theComponenents’s filteredArrayUsingPredicate:thePred)’s firstObject() as text
  
return aRes
end searchClassInFrameworks

★Click Here to Open This Script 

Posted in 未分類 | Tagged 10.11savvy 10.12savvy 10.13savvy | 1 Comment

2月1日が日曜日でうるう年ではないかチェック(ASOC)

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:2月1日が日曜日でうるう年ではないかチェック(ASOC)
— Created 2015-02-02 by Shane Stanley
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
–use framework "ASObjCExtras"

set resYList to {} –hit year

set theNSCalendar to current application’s NSCalendar’s currentCalendar() — do *not* use initWithCalendarIdentifier:

repeat with y from 2000 to 2100
  set aMlen to getMlenAndVerifyFirstDaysNum(y, 2, theNSCalendar, 28, 1) of me
  
if aMlen = true then
    set the end of resYList to y
  end if
  
–set the end of resYList to aMlen
end repeat

return resYList
–>  {​​​​​2009, ​​​​​2015, ​​​​​2026, ​​​​​2037, ​​​​​2043, ​​​​​2054, ​​​​​2065, ​​​​​2071, ​​​​​2082, ​​​​​2093, ​​​​​2099​​​}

on getMlenAndVerifyFirstDaysNum(aYear, aMonth, theNSCalendar, dMax, fdayNum)
  
  
–現在のLocaleのCalendarで指定年月の日数をかぞえる
  
set theDate to theNSCalendar’s dateWithEra:1 |year|:aYear |month|:aMonth |day|:1 hour:0 minute:0 |second|:0 nanosecond:0
  
set theResult to theNSCalendar’s rangeOfUnit:(current application’s NSDayCalendarUnit) inUnit:(current application’s NSMonthCalendarUnit) forDate:theDate
  
–>  {location:1, length:31}
  
set mLen to |length| of theResult
  
  
–指定年月の1日を取得
  
set theDay to theNSCalendar’s components:((current application’s NSWeekdayCalendarUnit) + (current application’s NSMonthCalendarUnit as integer)) fromDate:theDate
  
  
–指定年月の1日の曜日が指定曜日で、うるう月でなく、指定年月の日数が指定日数(dMax)であるか判定
  
if (theDay’s |weekday|() = fdayNum) and (theDay’s isLeapMonth() is false) and (mLen = dMax) then
    return true
  else
    return false
  end if
  
end getMlenAndVerifyFirstDaysNum

★Click Here to Open This Script 

Posted in 未分類 | Tagged 10.11savvy 10.12savvy 10.13savvy Calendar | Leave a comment

InputManagerのじっけん

Posted on 2月 6, 2018 by Takaaki Naganoya

–> InputManager.framework (To ~/Library/Frameworks)

AppleScript名:InputManagerのじっけん
— Created 2017-01-22 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "InputManager"

set cList to current application’s CSInputSource’s all()
–>  (NSArray) {​​​​​(CSInputSource) <CSInputSource: 0x60000000d310>, ​​​​​(CSInputSource) <CSInputSource: 0x60000000f580>, ​​​​​(CSInputSource) <CSInputSource: 0x60000000bd30>, ​​​​​(CSInputSource) <CSInputSource: 0x600000216d20>, ​​​​​(CSInputSource) <CSInputSource: 0x6000002168d0>, ​​​​​(CSInputSource) <CSInputSource: 0x600000216c50>, ​​​​​(CSInputSource) <CSInputSource: 0x600000012620>, ​​​​​(CSInputSource) <CSInputSource: 0x60000000b010>, ​​​​​(CSInputSource) <CSInputSource: 0x600000217010>​​​}

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

set c1 to (current application’s CSInputSource’s currentKeyboard()’s localizedName()) as string
–> "ひらがな"
–> "英字"

set c2 to (current application’s CSInputSource’s currentKeyboardLayout()’s localizedName()) as string
–> "U.S."

set err1 to (current application’s CSInputSource’s forLanguage:"ja")’s |select|()
log err1
set err2 to (current application’s CSInputSource’s forLanguage:"en_US")’s |select|()
log err2

★Click Here to Open This Script 

Posted in Input Method 未分類 | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

全国地方公共団体コードのチェックサム算出 v2

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:全国地方公共団体コードのチェックサム算出 v2
set chkSum to chkModulas11("01100") of me

–全国地方公共団体コードのチェックサム算出
on chkModulas11(aCodeNumStr)
  
  
if length of (aCodeNumStr as string) is not equal to 5 then return false
  
set cList to characters of (aCodeNumStr as string)
  
set aMultNum to 6
  
set aRes to 0
  
  
–第1桁から第5桁までの数字に、それぞれ6.5.4.3.2を乗じて算出した積の和を求め
  
repeat with i in cList
    set j to i as number
    
set aRes to aRes + j * aMultNum
    
set aMultNum to aMultNum – 1
  end repeat
  
  
if aRes < 11 then
    –ただし、積の和が11より小なるときは、検査数字は、11から積の和を控除した数字とする
    
set eRes to 11 – aRes
  else
    –通常パターン
    
–その和を11で除し、商と剰余(以下「余り数字」という。)を求めて
    
set cRes to aRes mod 11 –余りだけでよいのでは? 商を求めなくても余りは計算可能
    
set dRes to 11 – cRes
    
set eRes to last character of (dRes as string) –下1桁の数字を検査数字とする    
  end if
  
  
return eRes as number
end chkModulas11

★Click Here to Open This Script 

Posted in 未分類 | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

JANコードのチェックデジットの計算

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:JANコードのチェックデジットの計算
set aData to "240469540245" –女性用カジュアルパンプス(黒)
set aSum to calcJANcodeChkSum(aData)
–> 9

set aData to "978479731648" –「Mac使いへの道」
set aSum to calcJANcodeChkSum(aData)
–> 3

–JANコードのチェックデジットの計算
–参照元:http://www.kabukoba.co.jp/info/barcode/check.htm
on calcJANcodeChkSum(aData)
  set aList to characters of aData
  
  
if length of aList is not equal to 12 then return false
  
  
set kiSnum to 0
  
set guNum to 0
  
  
repeat with i from 2 to length of aList
    
    
set kiSuF to ((i – 1) mod 2)
    
    
set j to (item i of aList) as number
    
    
if kiSuF = 1 then
      –奇数
      
set kiSnum to kiSnum + j
    else
      –偶数
      
set guNum to guNum + j
    end if
    
  end repeat
  
  
set kiSnum to kiSnum * 3
  
set guNum to guNum + (first item of aList) as number
  
  
set totalNum to kiSnum + guNum
  
  
set totalChar to totalNum as string
  
set lastDig to (last character of totalChar) as number
  
if lastDig is not equal to 0 then
    set sumNum to 10 – lastDig
  else
    set sumNum to 0
  end if
  
  
return sumNum
  
end calcJANcodeChkSum

★Click Here to Open This Script 

Posted in 未分類 | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

アプリケーションのBundle IDからアプリのAppleScript対応度を取得する

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:アプリケーションのBundle IDからアプリのAppleScript対応度を取得する
— Created 2016-02-08 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

set aBundleID to "com.apple.Safari"
set aPath to retAppScriptabilityFromBundleID(aBundleID) of me
–>  true

–Bundle IDからアプリケーションのScriptabilityをbooleanで返す
on retAppScriptabilityFromBundleID(aBundleID)
  set appPath to current application’s NSWorkspace’s sharedWorkspace()’s absolutePathForAppBundleWithIdentifier:aBundleID
  
if appPath = missing value then return false
  
set aDict to (current application’s NSBundle’s bundleWithPath:appPath)’s infoDictionary()
  
set aRes to aDict’s valueForKey:"NSAppleScriptEnabled"
  
if aRes = missing value then return false
  
return aRes as boolean
end retAppScriptabilityFromBundleID

★Click Here to Open This Script 

Posted in 未分類 | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

アプリケーションのBundle IDからアプリアイコンへのフルパスを取得する

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:アプリケーションのBundle IDからアプリアイコンへのフルパスを取得する
— Created 2016-02-08 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

set aBundleID to "com.apple.Safari"
set aPath to retAppIconPathFromBundleID(aBundleID) of me
–>  "/Applications/Safari.app/Contents/Resources/compass.icns"

–Bundle IDからアプリケーションアイコンのフルパスをPOSIX pathで返す
on retAppIconPathFromBundleID(aBundleID)
  –Bundle IDからアプリケーションへのパスを取得する
  
set appPath to current application’s NSWorkspace’s sharedWorkspace()’s absolutePathForAppBundleWithIdentifier:aBundleID
  
if appPath = missing value then return false
  
  
–Info.plistを読み込んで、iconのファイル名を取得
  
set aDict to (current application’s NSBundle’s bundleWithPath:appPath)’s infoDictionary()
  
set iconFile to aDict’s valueForKey:"CFBundleIconFile"
  
if iconFile = missing value then return false
  
  
–パスからバンドルを取得して、バンドル内の指定ファイルタイプのファイル名のパスを取得する
  
set aBundle to current application’s NSBundle’s bundleWithPath:appPath
  
set iconFullPath to aBundle’s pathForResource:iconFile ofType:"icns"
  
if iconFullPath = missing value then return false
  
  
return iconFullPath as string
  
end retAppIconPathFromBundleID

★Click Here to Open This Script 

Posted in 未分類 | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

指定ファイルをオープンするデフォルト・アプリケーションのパスを求める

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:指定ファイルをオープンするデフォルト・アプリケーションのパスを求める
— Created 2017-11-07 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

set aFile to POSIX path of (choose file)
set bPath to getDefaultAppPathFromDocumentPath(aFile) of me
–>  "/Applications/Skim.app"

on getDefaultAppPathFromDocumentPath(aFile)
  set aURL to current application’s |NSURL|’s fileURLWithPath:aFile
  
set aWorkspace to current application’s NSWorkspace’s sharedWorkspace()
  
set anApp to aWorkspace’s URLForApplicationToOpenURL:aURL
  
if anApp = missing value then return false
  
return (anApp’s |path|()) as string
end getDefaultAppPathFromDocumentPath

★Click Here to Open This Script 

Posted in 未分類 | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

指定ファイルをオープンするデフォルト・アプリケーションのBundle IDを求める

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:指定ファイルをオープンするデフォルト・アプリケーションのBundle IDを求める
— Created 2017-11-07 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

set aFile to POSIX path of (choose file)
set anID to getDefaultAppIDFromDocumentPath(aFile) of me
–> "com.apple.Preview"

on getDefaultAppIDFromDocumentPath(aFile)
  set aURL to current application’s |NSURL|’s fileURLWithPath:aFile
  
set aWorkspace to current application’s NSWorkspace’s sharedWorkspace()
  
set appURL to aWorkspace’s URLForApplicationToOpenURL:aURL
  
set aBundle to current application’s NSBundle’s bundleWithURL:appURL
  
if aBundle = missing value then return false
  
set anID to aBundle’s bundleIdentifier()
  
return anID as string
end getDefaultAppIDFromDocumentPath

★Click Here to Open This Script 

Posted in 未分類 | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

Bundle IDで指定したアプリケーションのURL Schemeを求める

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:Bundle IDで指定したアプリケーションのURL Schemeを求める
— Created 2017-07-23 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"
use BridgePlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html

property |NSURL| : a reference to current application’s |NSURL|
property NSBundle : a reference to current application’s NSBundle
property SMSForder : a reference to current application’s SMSForder

load framework

set aRes to getURLSchemesFromBundleID("com.apple.Safari") of me
–>  {​​​​​appName:"Safari", ​​​​​appBundleID:"com.apple.Safari", ​​​​​urlScheme:{​​​​​​​"http", ​​​​​​​"https", ​​​​​​​"file"​​​​​}​​​}

on getURLSchemesFromBundleID(aBundleID)
  set aURL to current application’s NSWorkspace’s sharedWorkspace()’s URLForApplicationWithBundleIdentifier:aBundleID
  
if aURL = missing value then return false
  
  
set bPath to (aURL’s |path|()) as string
  
set aURLrec to getAppURLSchemes(bPath) of me
  
  
return aURLrec
end getURLSchemesFromBundleID

on getAppURLSchemes(aP)
  set aURL to |NSURL|’s fileURLWithPath:(POSIX path of aP)
  
set aBundle to NSBundle’s bundleWithURL:aURL
  
set aDict to aBundle’s infoDictionary()
  
  
set appNameDat to (aDict’s valueForKey:"CFBundleName") as string
  
set bundleIDat to (aDict’s valueForKey:"CFBundleIdentifier") as string
  
  
set urlSchemes to (aDict’s valueForKey:"CFBundleURLTypes")
  
if urlSchemes is not equal to missing value then
    set urlList to urlSchemes’s valueForKey:"CFBundleURLSchemes"
    
set urlListFlat to (SMSForder’s arrayByFlattening:urlList) as list
  else
    set urlListFlat to {}
  end if
  
  
set aRec to {appName:appNameDat, appBundleID:bundleIDat, urlScheme:urlListFlat}
  
return aRec
end getAppURLSchemes

★Click Here to Open This Script 

Posted in 未分類 | 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)
  • 新発売:AppleScriptからSiriを呼び出そう!
  • iWork 12.2がリリースされた
  • 従来と異なるmacOS 13の性格?
  • 新発売:CotEditor Scripting Book with AppleScript
  • macOS 13対応アップデート:AppleScript実践的テクニック集(1)GUI Scripting
  • macOS 13でNSNotFoundバグふたたび
  • ChatGPTでchatに対する応答文を取得
  • 新発売:iWork Scripting Book with AppleScript
  • Finderの隠し命令openVirtualLocationが発見される
  • macOS 13.1アップデートでスクリプトエディタの挙動がようやくまともに
  • あのコン過去ログビューワー(暫定版)
  • AppleScriptの数値変数で指数表示にならない最大値、最小値
  • バカスタム App選手権 入賞!

Tags

10.11savvy (1101) 10.12savvy (1242) 10.13savvy (1390) 10.14savvy (586) 10.15savvy (434) 11.0savvy (277) 12.0savvy (186) 13.0savvy (59) 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年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