use AppleScript version "2.4" — Yosemite (10.10) or later use framework "Foundation" use scripting additions
set aPath to POSIX path of (choose file of type {"com.apple.application-bundle"}) set aURL to (current application’s |NSURL|’s fileURLWithPath:aPath) set exResList to getDocumentTypesExtensionsFromPath(aURL) of me
on getDocumentTypesExtensionsFromPath(aURL) set aRes to getDocumentTypesFromAppURL(aURL) of me
set exRes to {} repeat with i in (aRes as list) set j to (current application’s NSDictionary’s dictionaryWithDictionary:(contents of i)) set tmpExt to (j’s valueForKey:"CFBundleTypeExtensions")
if tmpExt is not equal to missing value then set tmpExt to tmpExt as list repeat with ii in tmpExt set jj to "." & (contents of ii) if (jj is not in exRes) and (jj is not equal to ".*") and (jj is not equal to ".???") then set the end of exRes to jj end if end repeat end if end repeat
return exRes end getDocumentTypesExtensionsFromPath
on getDocumentTypesFromAppURL(aURL) set aBundle to current application’s NSBundle’s bundleWithURL:aURL if aBundle = missing value then return {} set aInfo to aBundle’s infoDictionary() if aInfo = missing value then return {} set aRes to aInfo’s objectForKey:"CFBundleDocumentTypes" –Document Types if aRes = missing value then return {} return aRes end getDocumentTypesFromAppURL
本サンプルについて、当初はステータスバー上のステータスアイテムからPopoverを表示させようかと考えたのですが、menu bar extra(ステータスアイテム)からPopoverは表示できないんですね(Uni Detectorのときに実験してうまく行かず、github上で公開している人のコードも眺めてみましたが……上から下まで全部自前で作っているようでした)。
script AppDelegate property parent : class "NSObject"
— IBOutlets property theWindow : missing value property theView : missing value property aButton : missing value
property aFlag : true property aStatusItem : missing value
property aMenu : missing value property bMenuItem : missing value
on applicationWillFinishLaunching:aNotification aButton’s setTitle:"–> Status Menu" set aFlag to true theWindow’s setContentView:theView end applicationWillFinishLaunching:
on applicationShouldTerminate:sender — Insert code here to do any housekeeping before your application quits return current application’s NSTerminateNow end applicationShouldTerminate:
on clicked:aSender set aTag to (tag of aSender) as integer
if aTag = 100 then if aFlag = true then –通常Window表示→Menu表示 my makeStatusItem() theWindow’s performClose:aSender aButton’s setTitle:"–> Window" else –Popup表示→通常Window表示
–Created By Shane Stanley –Modified By Takaaki Naganoya use AppleScript version "2.4" use scripting additions use framework "Foundation"
property |NSURL| : a reference to current application’s |NSURL| property NSOrderedSet : a reference to current application’s NSOrderedSet property NSURLTagNamesKey : a reference to current application’s NSURLTagNamesKey
set anAlias to (choose file) clearTagsForPath(anAlias) of me –delete tags set aRes to getTagsForPath(anAlias) of me –check
— clear all tags on clearTagsForPath(anAlias) set aURL to |NSURL|’s fileURLWithPath:(POSIX path of anAlias) aURL’s setResourceValue:{} forKey:(NSURLTagNamesKey) |error|:(missing value) end clearTagsForPath
— get the tags on getTagsForPath(anAlias) set aURL to |NSURL|’s fileURLWithPath:(POSIX path of anAlias) set {theResult, theTags} to aURL’s getResourceValue:(reference) forKey:(NSURLTagNamesKey) |error|:(missing value) if theTags = missing value then return {} — because when there are none, it returns missing value return theTags as list end getTagsForPath
— set the tags, replacing any existing on setTagsForPath(tagList, anAlias) set aURL to |NSURL|’s fileURLWithPath:(POSIX path of anAlias) aURL’s setResourceValue:tagList forKey:(NSURLTagNamesKey) |error|:(missing value) end setTagsForPath
— add to existing tags on addTagsForPath(tagList, anAlias) set aURL to |NSURL|’s fileURLWithPath:(POSIX path of anAlias) — get existing tags set {theResult, theTags} to aURL’s getResourceValue:(reference) forKey:(NSURLTagNamesKey) |error|:(missing value) if theTags ≠ missing value then — add new tags set tagList to (theTags as list) & tagList set tagList to (NSOrderedSet’s orderedSetWithArray:tagList)’s allObjects() — delete any duplicates end if aURL’s setResourceValue:tagList forKey:(NSURLTagNamesKey) |error|:(missing value) end addTagsForPath
use AppleScript version "2.4" — Yosemite (10.10) or later use framework "Foundation" use scripting additions
set resolList to {1024, 512, 256, 128, 64, 32, 16} set aTargFileBase to (choose file name with prompt "Select Export base name") as string
tell application "Pixelmator Pro" if (exists of document 1) = false then display dialog "There is no document" buttons {"OK"} default button 1 with icon 1 return end if
tell the front document set aWidth to width set aHeight to height
if {aWidth, aHeight} is not equal to {1024.0, 1024.0} then display dialog "Wrong Image Size (1024×1024 required)" buttons {"OK"} default button 1 with icon 2 with title "Size Error" return end if
repeat with i in resolList resize image width i height i resolution 72 algorithm bilinear export to file (aTargFileBase & "_" & (i as string) & "x" & (i as string) & ".png") as PNG undo end repeat end tell end tell
tell application "UnicodeChecker" properties of current code point –> {bidi mirrored:false, containing plane:plane id 0 of application "UnicodeChecker", id:12354, line break:"ID", assigned:true, canonical combining class description:"Not_Reordered", unicode name:"HIRAGANA LETTER A", assigned to abstract character:true, code point type:Graphic, class:code point, bidi class description:"Left_To_Right", script name:"Hiragana", general category description:"Other_Letter", bidi class:"L", containing block:block "Hiragana" of application "UnicodeChecker", general category:"Lo", name:"あ", canonical combining class:0} end tell
–> Script creates an event and adds invitees. Please modify the email to test account before running the script set theStartDate to (current date) set hours of theStartDate to 15 set minutes of theStartDate to 0 set seconds of theStartDate to 0 set theEndDate to theStartDate + (1 * hours) tell application "Calendar" tell calendar "calendar" make new event with properties {summary:"Apple Script Invitations", start date:theStartDate, end date:theEndDate, location:"one Infinite Loop"}
set theEvent to (first event where its summary = "Apple Script Invitations") tell theEvent make new attendee at end of attendees with properties {display name:"test", email:"caltest_as1@icloud.com"} end tell end tell end tell
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dictionary SYSTEM "file://localhost/System/Library/DTDs/sdef.dtd">
<dictionary xmlns:xi="http://www.w3.org/2003/XInclude">
<!-- We override some standard commands to add several optional properties. -->
<suite name="Standard Suite" code="????" description="Common classes and commands for all applications.">
<xi:include href="file://localhost/System/Library/ScriptingDefinitions/CocoaStandard.sdef" xpointer="xpointer(/dictionary/suite/node()[not((self::command and (@name = 'open')) or (self::class and (@name = 'window')))])"/>
set aScore to 0 set aMiss to 0 set maxMole to 3 —change mole attack number set delayCount to 300 –delay speed. Faster machine needs larger number.
tell application "Pixelmator Pro" activate if (exists of document "Piyomaru Whack-3-moles Game") = false then display dialog "There is no Piyomaru document" buttons {"OK"} default button 1 with icon 1 return end if
tell document "Piyomaru Whack-3-moles Game" set visible of layer "Mole1" to false set visible of layer "Mole2" to false set visible of layer "Mole3" to false set visible of layer "Mole3Hit" to false set visible of layer "Mole3Miss" to false
repeat 10 times tell layer "Title" set its visible to true set keyRes to keyscanWait(delayCount, true) of me set its visible to false set keyRes to keyscanWait(delayCount, true) of me if keyRes = true then exit repeat end tell end repeat
delay 1
set moleList to {{"Mole1", false, 20}, {"Mole2", false, 10}, {"Mole3", true, 20}} repeat maxMole times repeat with i in moleList copy i to {layerName, hitLogic, delayTime} set visible of layer layerName to true set keyRes to keyscanWait((random number from 1 to delayTime) * delayCount, hitLogic) of me set visible of layer layerName to false
if keyRes = true then set hitF to true set aScore to aScore + 1 beep set visible of layer "Mole3Hit" to true delay 1 set visible of layer "Mole3Hit" to false exit repeat else if keyRes = false then set aMiss to aMiss + 1 repeat 3 times set visible of layer "Mole3Miss" to true delay 0.5 set visible of layer "Mole3Miss" to false delay 0.5 end repeat set keyRes to true –To Skip See off action exit repeat end if end repeat
if keyRes is not equal to true then set aMiss to aMiss + 1 repeat 3 times set visible of layer "Mole3Miss" to true delay 0.5 set visible of layer "Mole3Miss" to false delay 0.5 end repeat end if end repeat
display dialog "Score:" & (aScore as string) & return & "Miss:" & (aMiss as string) with title "GAME OVER" buttons {"OK"} default button 1 with icon 1
end tell end tell
on keyscanWait(delayLoop, hitLogic) repeat delayLoop times set commandStatus to not ((((current application’s NSEvent’s modifierFlags() as integer) div (current application’s NSShiftKeyMask as integer)) mod 2) = 0) if commandStatus = true then return hitLogic end repeat return missing value end keyscanWait
use AppleScript version "2.4" — Yosemite (10.10) or later use framework "Foundation" use scripting additions
property |NSURL| : a reference to current application’s |NSURL| property NSMetadataItem : a reference to current application’s NSMetadataItem
set remoteMachineName to "MacMini2014.local" set remoteUserName to "maro"
–ScriptのCommentに書いておいたパスワードを拾って使う set mePath to (path to me)
–FinderからCommentは拾えるが、メタデータ経由で取得する処理も試してみた set remoteUserPass to getFinderComment(POSIX path of mePath) of me
tell application "Safari" if running then tell front document set aURL to URL –最前面のウィンドウがYouTubeの場合のみ処理 if aURL does not start with "https://www.youtube.com/" then display notification "エラー: YouTubeを再生していないため、ハンズオーバーしませんでした" return end if
–再生中のポジションを取得 set tRes to (do JavaScript "document.querySelector(’#movie_player video’).currentTime;")
–再生状況を取得 set pRes to (do JavaScript "document.querySelector(’#movie_player video’).paused;")
if pRes = false then –再生中であればPauseする set aRes to (do JavaScript "document.querySelector(’#movie_player .ytp-play-button’).click();") end if
openYouTubeOnRemoteMachine(remoteUserName, remoteUserPass, remoteMachineName, tRes, aURL) of me end tell end if end tell
–指定のリモートマシン上のSafariでYouTubeの指定ムービーの指定箇所からの再生を行う on openYouTubeOnRemoteMachine(remoteUser, remotePass, remoteMachineLocal, newDuration, newURL) set remoteMachineName to "eppc://" & remoteUser & ":" & remotePass & "@" & remoteMachineLocal
–URLの加工。ちょっと手抜きをした if newDuration is not 0 then set tText to retTimeText(newDuration) of me if newURL contains "&" then set sepChar to "?" else set sepChar to "&" end if
set newURL to newURL & sepChar & "t=" & tText end if
using terms from application "Safari" tell application "Safari" of machine remoteMachineName if not running then –起動していなかったらあらためてSafariを起動 launchRemoteSafari(remoteMachineName) of me end if
try close every document end try
set aWin to make new document
tell aWin set URL to newURL –フルスクリーン再生をためしてみたが、こういう書き方ではなかった模様(URLオープンを待つ必要もある) –set aRes to (do JavaScript "document.querySelector(’#movie_player playFullscreen’).click();") end tell
end tell end using terms from end openYouTubeOnRemoteMachine
–リモートマシン上でSafariを起動する on launchRemoteSafari(aMachine) using terms from application "Finder" tell application "Finder" of machine aMachine open application file id "com.apple.Safari" end tell end using terms from end launchRemoteSafari
–数値を「h」「m」「s」でフォーマットして返す on retTimeText(aTime) set aHour to aTime div 3600 set aMinute to (aTime – (aHour * 3600)) div 60 set aSeconds to (aTime mod 60)
set aString to ""
if aHour > 0 then set aString to aHour & "h" end if
if aMinute > 0 then set aString to aString & (aMinute as integer) & "m" end if
if aSeconds > 0 then set aString to aString & (aSeconds as integer as string) & "s" end if
return (aString as string) end retTimeText
–Finderコメントをメタデータ経由で取得 on getFinderComment(aPOSIX) set aURL to |NSURL|’s fileURLWithPath:aPOSIX set aMetaInfo to NSMetadataItem’s alloc()’s initWithURL:aURL set metaDict to (aMetaInfo’s valuesForAttributes:{"kMDItemFinderComment"}) as record if metaDict = {} then return "" set aComment to kMDItemFinderComment of (metaDict) return aComment end getFinderComment
Classic Mac OSの頃は「しかたなく」クリップボードを使って行う処理がありましたが、最近(Mac OS X移行後)では極力使わないようにしています。文字コード変換、スタイル付きテキストのプレーンテキスト化などクリップボードを経由しないとできない処理がたくさんありました。画像の形式変換ですらクリップボードを経由して処理していた記憶があります。
クリップボードにデータを入れる「set the clipboad」、クリップボードからデータを取り出す「the clipboard」、クリップボードの情報を取得する「clioboard info」などがあります。スピードも遅くないのですが、本コマンドでクリップボード内容を捕捉できるまでに(クリップボードの内容が反映されるまでに)若干時間がかかります。
–Created 2015-08-03 by Shane Stanley use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" — for NSPasteboard
my restoreClipboard:{"ABC"}
–クリップボードに内容を設定する on restoreClipboard:theArray — get pasteboard set thePasteboard to current application’s NSPasteboard’s generalPasteboard()
— clear it, then write new contents thePasteboard’s clearContents() thePasteboard’s writeObjects:theArray end restoreClipboard:
use AppleScript version "2.4" — Yosemite (10.10) or later use framework "Foundation" use scripting additions
activate application "Safari" tell application "System Events" tell process "Safari" click menu item "コピー" of menu 1 of menu bar item "編集" of menu bar 1 end tell end tell
–Initialize set curPosX to 1 set curPosY to 1 set curMax to 30
set rText to "" repeat curMax times set rText to rText & "🛑" end repeat
tell application "CotEditor" write to console "Real-time Keyscan test with Modifier Keys [ Control ] : Move Left [ Shift ] : Move Right [ Option ] : Move Up [ Command ] : Move Down
[ Caps Lock ] : Quit this script"
make new document activate end tell
–Main Loop repeat set leftF to my checkModifier:"control" set rightF to my checkModifier:"shift" set upF to my checkModifier:"option" set downF to my checkModifier:"command"
set quitF to my checkModifier:"caps"
if quitF = true then return
if leftF = true then if curPosX > 1 then set curPosX to curPosX – 1 end if else if rightF = true then if curPosX ≤ curMax then set curPosX to curPosX + 1 end if end if
if upF = true then if curPosY > 1 then set curPosY to curPosY – 1 end if else if downF = true then if curPosY < curMax then set curPosY to curPosY + 1 end if end if
–make display text set aText to "" repeat with i from 1 to curMax + 1 if i = curPosX then set aText to aText & "🛑" else set aText to aText & "㍳" end if end repeat
set bText to "" repeat with y from 1 to curMax if y = curPosY then set bText to bText & rText & return else set bText to bText & aText & return end if end repeat
my displayText(bText)
end repeat
–テキスト画面描画 on displayText(aText) tell application "CotEditor" tell front document set contents to aText end tell end tell end displayText
–複数同時検出に対応 on checkModifier:keyName if keyName = "option" then set theMask to current application’s NSAlternateKeyMask as integer else if keyName = "control" then set theMask to current application’s NSControlKeyMask as integer else if keyName = "command" then set theMask to current application’s NSCommandKeyMask as integer else if keyName = "shift" then set theMask to current application’s NSShiftKeyMask as integer else if keyName = "caps" then set theMask to current application’s NSEventModifierFlagCapsLock as integer else if keyName = "num" then set theMask to current application’s NSEventModifierFlagNumericPad as integer else if keyName = "help" then set theMask to current application’s NSEventModifierFlagHelp as integer else if keyName = "fn" then set theMask to current application’s NSEventModifierFlagFunction as integer else return false end if
set theFlag to current application’s NSEvent’s modifierFlags() as integer if ((theFlag div theMask) mod 2) = 0 then return false else return true end if end checkModifier: