Finder上で選択中のAppleScriptバンドル形式書類をスクリプトエディタでオープンして、scpt形式で保存し直すAppleScriptです。
スクリプトエディタのコンテクストメニューが.scpt形式しか認識しないので、バンドル形式のScriptを変換するために作ったものです。
Finderの選択中のファイルをフィルタするのに、フィルタ参照ではなくUTIを求めて判定しています。
「AppleScript最新リファレンスv2.8対応」の付録Scriptの加工用に作ったものです。バンドル形式への変換は書いてあったものの、scpt形式に変換するものは書いていなかったので急遽、用意しました。
AppleScript名:Finder上で選択中のASバンドル書類をオープンしてscpt形式で再保存.scptd |
— – Created by: Takaaki Naganoya – Created on: 2023/10/20 — – Copyright © 2023 Piyomaru Software, All Rights Reserved — use AppleScript version "2.4" use scripting additions use framework "Foundation" property |NSURL| : a reference to current application’s |NSURL| property NSArray : a reference to current application’s NSArray property NSPredicate : a reference to current application’s NSPredicate property NSURLTypeIdentifierKey : a reference to current application’s NSURLTypeIdentifierKey on run set aResList to main("com.apple.applescript.script-bundle") of me set ngList to {} repeat with i in aResList set anAlias to contents of i set aRes to saveAppleScriptAsScript(anAlias) of me if aRes = false then set the end of ngList to contents of i end if end repeat return ngList end run –scptdファイルをaliasで渡すと、scptファイルで同じ場所に保存し直す on saveAppleScriptAsScript(aFile) set dPOSIX to POSIX path of aFile if dPOSIX ends with "/" then set dPOSIX to (strings 1 thru -2 of dPOSIX) end if –フルパスから、拡張子を付け替える set newPath to changeExtFromPath(dPOSIX, "scpt") of me set newFile to POSIX file newPath tell application id "com.apple.scripteditor2" try open aFile tell front document check syntax end tell save front document as "script" in file newFile close front document without saving return true on error return false end try end tell end saveAppleScriptAsScript –POSIX pathの拡張子を付け替える on changeExtFromPath(aPath, newExt) set pathString to current application’s NSString’s stringWithString:aPath set newPath to ((pathString’s stringByDeletingPathExtension())’s stringByAppendingPathExtension:newExt) as string return newPath end changeExtFromPath on main(acceptUTI) tell application "Finder" set aSel to selection as alias list end tell set aList to {} repeat with i in aSel set anAlias to contents of i set aUTI to getUTIfromPath(anAlias) of me if aUTI is not equal to missing value then set uRes to filterUTIList({aUTI}, acceptUTI) of me if uRes is not equal to {} then set the end of aList to contents of i end if end if end repeat return aList end main –AliasからUTIを求める on getUTIfromPath(anAlias) set aPOSIXpath to POSIX path of anAlias set aURL to current application’s |NSURL|’s fileURLWithPath:aPOSIXpath if aURL = missing value then return missing value set aRes to aURL’s resourceValuesForKeys:{current application’s NSURLTypeIdentifierKey} |error|:(missing value) if aRes = missing value then return missing value return (aRes’s NSURLTypeIdentifierKey) as string end getUTIfromPath –UTIリストが指定UTIに含まれているかどうか演算を行う on filterUTIList(aUTIList, aUTIstr) set anArray to NSArray’s arrayWithArray:aUTIList set aPred to NSPredicate’s predicateWithFormat_("SELF UTI-CONFORMS-TO %@", aUTIstr) set bRes to (anArray’s filteredArrayUsingPredicate:aPred) as list return bRes end filterUTIList |
More from my site
(Visited 39 times, 1 visits today)