–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
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')))])"/>
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
–再生中のポジションを取得 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:
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:
— Created 2020-09-27 by Takaaki Naganoya — 2020 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation"
–指定文字列の前後に存在するスペースと、文字列本体中に存在する2個以上の連続するスペースを削除して返す set a0Str to " First model, with mechanical scroll wheel. 10 GB model released later ." set aRes to removeWhiteSpaceRepeatation(a0Str, " ") of me –> "First model, with mechanical scroll wheel. 10 GB model released later ."
–クリーニング対象文字(たぶんスペース)が処理対象文字列の前後にあったり、途中に2つ以上連続して存在している場合には削除 on removeWhiteSpaceRepeatation(a0Str as string, aChar as string) if length of aChar is not equal to 1 then return false
set a1Str to trimFromHeadAndTail(a0Str, aChar) of me –> "First model, with mechanical scroll wheel. 10 GB model released later ."
set sucList to detectSuccsessionOfSpace(a1Str, aChar) of me –> {{43, 53}, {80, 108}}
if sucList = {} then return a1Str
set allRes to removeRepeatedSpaceChar(a1Str, sucList)
return allRes end removeWhiteSpaceRepeatation
–同一文字(スペース)の連続出現リスト({{start pos 1, end pos 1}, {start pos 2, end pos 2}……})をもとにテキストを切り抜く on removeRepeatedSpaceChar(a1Str as string, sucList as list) set aList to characters of a1Str
set aLen to length of aList set outStr to "" set aCount to 1 copy contents of item aCount of sucList to {sItem, eItem}
repeat with i in sucList copy i to {tmpS, tmpE} if aCount = tmpS then –Skip copy tmpE to aCount else set tmpStr to text (aCount) thru tmpS of a1Str set outStr to outStr & tmpStr copy tmpE to aCount end if end repeat
if tmpE < aLen then set tmpStr to text (tmpE + 1) thru -1 of a1Str set outStr to outStr & tmpStr end if
return outStr end removeRepeatedSpaceChar
–対象文字列で、2個以上同一文字(スペースを想定)が連続して存在する場合には削除する on detectSuccsessionOfSpace(a1Str as string, sucTargChar as string) set aList to characters of a1Str set aLen to length of aList set newList to {}
set tmpS to 0 set tmpE to 0 set spcF to false –false:spaceではない。true:spaceをみつけた set spcCount to 0
repeat with i from 1 to aLen set j to contents of item i of aList
if j = sucTargChar then –Space 1 char (must be a char)
if spcF = true then –スペースが連続している最中 set spcCount to spcCount + 1 else –スペースの連続部分(?)の先頭をフェッチした状態 set spcF to true set spcCount to 1 copy i to tmpS end if
else –連続スペースの末端部分 if spcF = true then copy i to tmpE if spcCount > 1 then set the end of newList to {tmpS, tmpE – 1} end if set spcCount to 0 set spcF to false else –何もしない end if end if
end repeat
return newList end detectSuccsessionOfSpace
–文字列の先頭と末尾から連続するスペースを検索して削除する on trimFromHeadAndTail(aStr as string, trimChar as string) set aLen to length of aStr set aList to characters of aStr
–Find not target character from head to tail set aCount to 1 repeat with i in aList set j to contents of i if j is not equal to trimChar then exit repeat end if set aCount to aCount + 1 end repeat
if aLen ≤ aCount then return ""
–Find not target character from tail to head set bCount to 1 repeat with ii in (reverse of aList) set jj to contents of ii if jj is not equal to trimChar then exit repeat end if set bCount to bCount + 1 end repeat
set resStr to text aCount thru (aLen – bCount + 1) of aStr return resStr end trimFromHeadAndTail
–Written By Philip Aker –文字置換ルーチン on repChar(origText as string, targStr as string, repStr as string) set {txdl, AppleScript’s text item delimiters} to {AppleScript’s text item delimiters, targStr} set temp to text items of origText set AppleScript’s text item delimiters to repStr set res to temp as text set AppleScript’s text item delimiters to txdl return res end repChar
on dispDiff(aStr, bStr, aSize) using terms from scripting additions set mePath to path to me set resPath to (mePath as string) & "Contents:Resources:index.html" set myStr to (read (resPath as alias) as «class utf8») as string end using terms from
set aString to current application’s NSString’s stringWithFormat_(myStr, aStr, bStr) as string set paramObj to {myMessage:"Browse diff", mySubMessage:"This is an mergely test", htmlStr:aString, viewSize:aSize}
webD’s displayWebDialog(paramObj) end dispDiff end script
–Works with Keynote v10.2 or later set aTagFile to ((path to desktop) as string) & "testOut.m4v"
tell application "Keynote" set exOpt to {compression factor:1.0, movie format:native size, class:export options} –set exOpt to {compression factor:1.0, movie format:native size, movie codec:h.264, class:export options}–this cause error export document 1 to file aTagFile as QuickTime movie with properties exOpt
個人的には信頼度の問題からmacOS 10.15は「パス」なので、macOS 11.0 Big SurがRelease時にコケないことを祈るのみです(メールが消えるとかいう話が一番OSの信頼度を下げる)。macOS 10.13, “Vista”という史上最悪・最低の大失敗をやらかして以来、「Release版のOSがBetaよりも低品質」という昨今のmacOS。10.13もBetaの時にはよかったのに、Release版で大崩壊を起こして見るも無残な姿に成り果てました。10.14もBetaの段階でGUI Scriptingの認証系でひどいバグがあって、「とても使えない」と判断。10.14.6まで待ってようやく手を出す気になったほど。
macOS 11.0, Big SurはBeta段階でも珍しく好印象で(GUIまわりの仕様はコロコロ変わるけど)、macOS 10.15みたいにBeta段階で「もう、これはダメ!」という話も聞きません(Relase時に紹介記事を書かなかったOSは10.15がはじめてです。10.15の崩壊度はひどかった)。macOS 11.0, Big Surには、macOS 10.13のような「Release版の大崩壊」を起こしてほしくないものです。