NSWorkspace

[Open]iBookの保存先を開く


AppleScript サンプルコード

【スクリプトエディタで開く】 |

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004# com.cocolog-nifty.quicktimer.icefloe
005----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
006use AppleScript version "2.8"
007use framework "Foundation"
008use framework "AppKit"
009use framework "UniformTypeIdentifiers"
010use scripting additions
011
012property refMe : a reference to current application
013property strBundleID : "com.apple.iBooksX"
014
015#ライブラリーフォルダ
016set appFileManager to refMe's NSFileManager's defaultManager()
017set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSLibraryDirectory) inDomains:(refMe's NSUserDomainMask))
018set ocidLibraryDirPathURL to ocidURLsArray's firstObject()
019#ユーザーデータ保存先
020set ocidLocalSaveDirPathURL to ocidLibraryDirPathURL's URLByAppendingPathComponent:("Mobile Documents/iCloud~com~apple~iBooks/Documents") isDirectory:(true)
021#ダウンロードデータ保存先
022set ocidCloudSaveDirPathURL to ocidLibraryDirPathURL's URLByAppendingPathComponent:("Containers/com.apple.iBooksX/Data") isDirectory:(true)
023
024
025
026set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
027set boolDone to appSharedWorkspace's openURL:(ocidLocalSaveDirPathURL)
028set boolDone to appSharedWorkspace's openURL:(ocidCloudSaveDirPathURL)
029
030return
AppleScriptで生成しました

|

Macでファイルを開くアプリを選択する(ファイルをデフォルトで開くアプリを設定・変更する)



ダウンロード - setdefaults.zip




ダイアログに
ファイルをドラッグ&ドロップでもOK

Photo_20241103140702Photo_20241103140701
AppleScript サンプルコード

【スクリプトエディタで開く】 |

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003# ファイルタイプのデフォルトのアプリケーションを変更します
004#com.cocolog-nifty.quicktimer.icefloe
005----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
006use AppleScript version "2.8"
007use framework "Foundation"
008use framework "UniformTypeIdentifiers"
009use framework "AppKit"
010use scripting additions
011property refMe : a reference to current application
012
013
014set strMes to ("ファイルの拡張子を入力\n入力欄に対象ファイルのドロップでもOK") as text
015
016
017##############################
018#####ダイアログ
019##############################
020###ダイアログを前面に出す
021set strName to (name of current application) as text
022if strName is "osascript" then
023  tell application "Finder" to activate
024else
025  tell current application to activate
026end if
027set aliasIconPath to POSIX file "/System/Library/CoreServices/Finder.app/Contents/Resources/Finder.icns" as alias
028try
029  set recordResult to (display dialog strMes with title "ファイルタイプのデフォルトのアプリケーションを変更します" default answer "" buttons {"OK", "キャンセル"} default button "OK" with icon aliasIconPath giving up after 20 without hidden answer) as record
030on error
031  log "エラーしました"
032  return
033end try
034if "OK" is equal to (button returned of recordResult) then
035  set strReturnedText to (text returned of recordResult) as text
036else if (gave up of recordResult) is true then
037  return "時間切れです"
038else
039  return "キャンセル"
040end if
041##############################
042#####戻り値整形
043##############################
044set ocidResponseText to (refMe's NSString's stringWithString:(strReturnedText))
045###タブと改行を除去しておく
046set ocidResponseText to (ocidResponseText's stringByReplacingOccurrencesOfString:("\n") withString:(""))
047set ocidResponseText to (ocidResponseText's stringByReplacingOccurrencesOfString:("\r") withString:(""))
048##############################
049#####パスドロップの場合
050##############################
051if (ocidResponseText as text) contains "/" then
052  set ocidFilePathStr to refMe's NSString's stringWithString:(ocidResponseText)
053  set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
054  set ocidResponseText to ocidFilePath's pathExtension()
055end if
056#カンマ取る
057set ocidResponseText to (ocidResponseText's stringByReplacingOccurrencesOfString:(".") withString:(""))
058
059##############################
060#####処理
061##############################
062set ocidExtensionTType to refMe's UTType's typeWithFilenameExtension:(ocidResponseText)
063set ocitExtensionUTI to ocidExtensionTType's identifier()
064
065###ワークスペース初期化
066set appShardWorkspace to refMe's NSWorkspace's sharedWorkspace()
067###UTTypeタイプのデフォルトアプリケーション
068set ocidAppPathURL to appShardWorkspace's URLsForApplicationsToOpenContentType:(ocidExtensionTType)
069
070################################################
071###起動ボリュームにあるURLのみにする
072###URL格納用の可変ARRAY
073set ocidChooseArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0
074ocidChooseArray's setArray:ocidAppPathURL
075###収集したURLの個数
076set numCntArray to count of ocidAppPathURL
077###収集したURLから外部ボリュームのものを削除
078###起動ボリュームの名前
079set strFilePath to "/System/Library/CoreServices/Finder.app" as text
080set ocidFinderFilePathStr to refMe's NSString's stringWithString:(strFilePath)
081set ocidFinderFilePath to ocidFinderFilePathStr's stringByStandardizingPath()
082set ocidFinderFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFinderFilePath) isDirectory:false)
083set listVolumeNameKey to (ocidFinderFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLVolumeNameKey) |error| :(reference))
084set strVolumeName to (item 2 of listVolumeNameKey) as text
085####収集したURLの数だけ繰り返し
086repeat numCntArray times
087  ###Arrayの削除なので後ろから処理
088  set itemAppPathURL to ocidChooseArray's objectAtIndex:(numCntArray - 1)
089  ###ボリューム名を取得して
090  set listPathVolumeName to (itemAppPathURL's getResourceValue:(reference) forKey:(refMe's NSURLVolumeNameKey) |error| :(reference))
091  set strPathVolumeName to (item 2 of listPathVolumeName) as text
092  ###外部ボリュームのものは削除
093  if strPathVolumeName is "Preboot" then
094    log strPathVolumeName
095  else if strPathVolumeName is not strVolumeName then
096    ocidChooseArray's removeObjectAtIndex:(numCntArray - 1)
097  end if
098  set numCntArray to numCntArray - 1
099end repeat
100
101
102################################################
103###アプリ名とURLのレコードを生成する
104###ダイアログ用のアプリケーション名リスト
105set listAppName to {} as list
106###アプリケーションのURLを参照させるためのレコード
107set ocidBrowserDictionary to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
108####################################################
109####UTTypeとして利用可能なアプリケーション一覧を取得する
110####################################################
111repeat with itemAppPathURL in ocidChooseArray
112  ###アプリケーションの名前
113  set listResponse to (itemAppPathURL's getResourceValue:(reference) forKey:(refMe's NSURLNameKey) |error| :(missing value))
114  set strAppName to (item 2 of listResponse) as text
115  log "ブラウザの名前は:" & strAppName & "です"
116  copy strAppName to end of listAppName
117  ####パス
118  set aliasAppPath to itemAppPathURL's absoluteURL() as alias
119  log "ブラウザのパスは:" & aliasAppPath & "です"
120  ####バンドルID取得
121  set ocidAppBunndle to (refMe's NSBundle's bundleWithURL:(itemAppPathURL))
122  set ocidBunndleID to ocidAppBunndle's bundleIdentifier
123  set strBundleID to ocidBunndleID as text
124  log "ブラウザのBunndleIDは:" & strBundleID & "です"
125  (ocidBrowserDictionary's setObject:(itemAppPathURL) forKey:(strAppName))
126end repeat
127################################
128#ソート
129set ocidAppNameArray to refMe's NSArray's arrayWithArray:(listAppName)
130set ocidSortedArray to ocidAppNameArray's sortedArrayUsingSelector:("localizedStandardCompare:")
131set listAppName to ocidSortedArray as list
132
133################################
134##ダイアログ
135################################
136###ダイアログを前面に
137tell current application
138  set strName to name as text
139end tell
140####スクリプトメニューから実行したら
141if strName is "osascript" then
142  tell application "Finder"
143    activate
144  end tell
145else
146  tell current application
147    activate
148  end tell
149end if
150set strPrompt to ("拡張子『" & ocidResponseText & "』をURLを開く\nデフォルトアプリケーションを選んでください") as text
151try
152  set listResponse to (choose from list listAppName with title "選んでください" with prompt strPrompt default items (item 1 of listAppName) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed)
153on error
154  log "エラーしました"
155  return "エラーしました"
156end try
157if listResponse is false then
158  return "キャンセルしました"
159end if
160set strResponse to (item 1 of listResponse) as text
161################################
162##アプリケーションのURLを取得する
163################################
164###アプリケーションのURLを取り出す
165set ocidAppPathURL to ocidBrowserDictionary's objectForKey:(strResponse)
166###選んだアプリケーションのバンドルID
167set ocidAppBunndle to (refMe's NSBundle's bundleWithURL:(ocidAppPathURL))
168set ocidBunndleID to ocidAppBunndle's bundleIdentifier
169###IF用にテキストにしておく
170set strBunndleID to ocidBunndleID as text
171
172
173################################
174##デフォルトに設定する
175################################
176set appShardWorkspace to refMe's NSWorkspace's sharedWorkspace()
177appShardWorkspace's setDefaultApplicationAtURL:(ocidAppPathURL) toOpenContentType:(ocidExtensionTType) completionHandler:(missing value)
178
179
180################################
181##設定後に 値の確定
182################################
183
184set strFilePath to "~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist"
185set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
186set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
187set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
188### PLISTを読み込む
189set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s initWithContentsOfURL:(ocidFilePathURL)
190###LSHandlers Array
191set ocidLSHandlersArray to ocidPlistDict's objectForKey:"LSHandlers"
192###項目数の数だけ繰り返し
193repeat with itemLSHandlers in ocidLSHandlersArray
194  ###コンテンツタイプを取得して
195  set ocidLSHandlerContentType to (itemLSHandlers's valueForKey:"LSHandlerContentType")
196  ###設定項目のコンテンツタイプと同じなら
197  set strLSHandlerContentType to ocidLSHandlerContentType as text
198  if strLSHandlerContentType is ocitExtensionUTI then
199    ###変更前の値
200    set ocidRoleViewer to (itemLSHandlers's valueForKey:"LSHandlerRoleViewer")
201    set ocidRoleAll to (itemLSHandlers's valueForKey:"LSHandlerRoleAll")
202    set ocidRoleAll to (itemLSHandlers's valueForKey:"LSHandlerRoleEditor")
203    set ocidRoleShell to (itemLSHandlers's valueForKey:"LSHandlerRoleShell")
204    log ocidRoleViewer as text
205    log ocidRoleAll as text
206    log ocidRoleAll as text
207    log ocidRoleShell as text
208    ####値を変更する
209    (itemLSHandlers's setValue:(strBunndleID) forKey:"LSHandlerRoleViewer")
210    (itemLSHandlers's setValue:(strBunndleID) forKey:"LSHandlerRoleAll")
211    (itemLSHandlers's setValue:(strBunndleID) forKey:"LSHandlerRoleEditor")
212    (itemLSHandlers's setValue:(strBunndleID) forKey:"LSHandlerRoleShell")
213    ####変更後の値
214    log (itemLSHandlers's valueForKey:"LSHandlerRoleViewer") as text
215    log (itemLSHandlers's valueForKey:"LSHandlerRoleAll") as text
216    log (itemLSHandlers's valueForKey:"LSHandlerRoleEditor") as text
217    log (itemLSHandlers's valueForKey:"LSHandlerRoleShell") as text
218  end if
219  
220end repeat
221###保存
222set boolDone to ocidPlistDict's writeToURL:(ocidFilePathURL) atomically:true
223log boolDone as boolean
224
225
226#############################
227###CFPreferencesを再起動
228#############################
229#####CFPreferencesを再起動させて変更後の値をロードさせる
230set strCommandText to "/usr/bin/killall cfprefsd" as text
231do shell script strCommandText
232
233
234#############################
235###LaunchServicesを再起動
236#############################
237####lsregister初期化
238(*#色々やってみたが不具合の方が多いので停止
239set strCommandText to "/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -kill -seed" as text
240do shell script strCommandText
241*)
242
243
244log "設定終了"
245return "設定終了"
AppleScriptで生成しました

|

バンドルIDからアプリケーションのインストールURLを取得


AppleScript サンプルコード

【スクリプトエディタで開く】 |

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#com.cocolog-nifty.quicktimer.icefloe
004#
005#バンドルIDからアプリケーションのパスを取得
006----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
007use AppleScript version "2.8"
008use framework "Foundation"
009use framework "AppKit"
010use framework "UniformTypeIdentifiers"
011use scripting additions
012property refMe : a reference to current application
013
014###########################
015#アプリケーションのパスの取得
016set strBundleID to ("com.aone.keka") as text
017set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
018set ocidAppBundle to (refMe's NSBundle's bundleWithIdentifier:(strBundleID))
019#見つからない対策
020if ocidAppBundle ≠ (missing value) then
021  set ocidAppPathURL to ocidAppBundle's bundleURL()
022else if ocidAppBundle = (missing value) then
023  set ocidAppPathURL to (appSharedWorkspace's URLForApplicationWithBundleIdentifier:(strBundleID))
024end if
025#見つからない対策2
026if ocidAppPathURL = (missing value) then
027  tell application "Finder"
028    try
029      set aliasAppApth to (application file id strBundleID) as alias
030      set strAppPath to (POSIX path of aliasAppApth) as text
031      set ocidAppPathStr to refMe's NSString's stringWithString:(strAppPath)
032      set ocidAppPath to ocidAppPathStr's stringByStandardizingPath()
033      set ocidAppPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidAppPath) isDirectory:(true)
034    on error
035      return "アプリケーションが見つかりませんでした"
036    end try
037  end tell
038end if
039set strAppPath to (ocidAppPathURL's |path|()) as text
AppleScriptで生成しました

|

新規インスタンスで起動させる


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
# ウィンドウとか前面とか管理が難しいのでお勧めしない方法ではある
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKIt"
use framework "UniformTypeIdentifiers"
use scripting additions

property refMe : a reference to current application


set strBundleID to "com.apple.Terminal"

set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()

##バンドルからアプリケーションのURLを取得
set ocidAppBundle to (refMe's NSBundle's bundleWithIdentifier:(strBundleID))
if ocidAppBundle ≠ (missing value) then
  set ocidAppPathURL to ocidAppBundle's bundleURL()
else if ocidAppBundle = (missing value) then
  set ocidAppPathURL to (appSharedWorkspace's URLForApplicationWithBundleIdentifier:(strBundleID))
end if
##予備(アプリケーションのURL)
if ocidAppPathURL = (missing value) then
  tell application "Finder"
    try
      set aliasAppApth to (application file id strBundleID) as alias
      set strAppPath to POSIX path of aliasAppApth as text
      set strAppPathStr to refMe's NSString's stringWithString:(strAppPath)
      set strAppPath to strAppPathStr's stringByStandardizingPath()
      set ocidAppPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(strAppPath) isDirectory:true
    on error
return "アプリケーションが見つかりませんでした"
    end try
  end tell
end if
#### 新しいインスタンスで起動する
set ocidConfig to refMe's NSWorkspaceOpenConfiguration's configuration()
ocidConfig's setActivates:(true)
ocidConfig's setAddsToRecentItems:(false)
ocidConfig's setCreatesNewApplicationInstance:(true)
set boolDone to appSharedWorkspace's openApplicationAtURL:(ocidAppPathURL) configuration:(ocidConfig) completionHandler:(missing value)



|

[selectFile:inFileViewerRootedAtPath:]ファイルを選択した状態でFinderで開く


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions
property refMe : a reference to current application

###選択するパス
set strFilePath to "~/Library/Fonts" as text
set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
###表示上のルートパス
set strDirPath to "~/Library" as text
set ocidDirPathStr to refMe's NSString's stringWithString:(strDirPath)
set ocidDirPath to ocidFilePathStr's stringByStandardizingPath()
log className() of ocidDirPath as text
###実行
set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
set boolDone to appSharedWorkspace's selectFile:(ocidFilePath) inFileViewerRootedAtPath:(ocidDirPath)
###結果
log boolDone
if boolDone is false then
  set aliasFilePath to (ocidFilePathURL's absoluteURL()) as alias
  tell application "Finder"
    open folder aliasCameraRawDirPathURL
  end tell
return "エラーしました"
end if

|

[Finder]make new Finder windowでパスセット

ダウンロード - _よく使う移動先.zip


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "Appkit"
use scripting additions

property refMe : a reference to current application

###設定先
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidTempDirURL to appFileManager's temporaryDirectory()

set ocidFilePath to ocidTempDirURL's |path|()
###開く
set ocidSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
set boolSelectFileResults to ocidSharedWorkspace's selectFile:(ocidFilePath) inFileViewerRootedAtPath:(ocidFilePath)
log boolSelectFileResults
if boolSelectFileResults = false then
  set aliasFilePath to (ocidTempDirURL's absoluteURL()) as alias
  tell application "Finder"
    set refNewWindow to make new Finder window
    tell refNewWindow
      set position to {10, 30}
      set bounds to {10, 30, 720, 480}
    end tell
    set target of refNewWindow to aliasFilePath
    set selection to aliasFilePath
  end tell
end if



【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "Appkit"
use scripting additions

property refMe : a reference to current application

###設定先
set strFilePath to "~/Library/Preferences" as text
set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:true)
set aliasFilePath to (ocidFilePathURL's absoluteURL()) as alias
###開く
set ocidSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
set boolSelectFileResults to ocidSharedWorkspace's selectFile:(ocidFilePath) inFileViewerRootedAtPath:(ocidFilePath)
if boolSelectFileResults = false then
  tell application "Finder"
    set refNewWindow to make new Finder window
    set target of refNewWindow to aliasFilePath
  end tell
  
end if




【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "Appkit"
use scripting additions

property refMe : a reference to current application

###設定先
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidTempDirURL to appFileManager's temporaryDirectory()
set aliasFilePath to (ocidTempDirURL's absoluteURL()) as alias
set ocidFilePath to ocidTempDirURL's |path|()
###開く
set ocidSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
set boolSelectFileResults to ocidSharedWorkspace's selectFile:(ocidFilePath) inFileViewerRootedAtPath:(ocidFilePath)
if boolSelectFileResults = false then
  tell application "Finder"
    set refNewWindow to make new Finder window
    set target of refNewWindow to aliasFilePath
  end tell
end if






|

[Preview]別ウィンドウで開く(setCreatesNewApplicationInstance)

『出来る』ってだけで、あまり推奨される方法では無いかな

【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKIt"
use framework "UniformTypeIdentifiers"
use scripting additions

property refMe : a reference to current application

property strBundleID : "com.apple.Preview"

set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
##バンドルからアプリケーションのURLを取得
set ocidAppBundle to (refMe's NSBundle's bundleWithIdentifier:(strBundleID))
if ocidAppBundle ≠ (missing value) then
  set ocidAppPathURL to ocidAppBundle's bundleURL()
else if ocidAppBundle = (missing value) then
  set ocidAppPathURL to (appSharedWorkspace's URLForApplicationWithBundleIdentifier:(strBundleID))
end if
##予備(アプリケーションのURL)
if ocidAppPathURL = (missing value) then
  tell application "Finder"
    try
      set aliasAppApth to (application file id strBundleID) as alias
      set strAppPath to POSIX path of aliasAppApth as text
      set strAppPathStr to refMe's NSString's stringWithString:(strAppPath)
      set strAppPath to strAppPathStr's stringByStandardizingPath()
      set ocidAppPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(strAppPath) isDirectory:true
    on error
return "アプリケーションが見つかりませんでした"
    end try
  end tell
end if

###ファイルA
set strFilePath to "/System/Library/Templates/Data/Library/User Pictures/Sports/8ball.heic"
set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
set ocidFilePathAURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)

###新規インスタンスで開く設定
set ocidConfig to refMe's NSWorkspaceOpenConfiguration's configuration()
ocidConfig's setActivates:(true)
ocidConfig's setAddsToRecentItems:(false)
ocidConfig's setCreatesNewApplicationInstance:(false)
###ファイルAを開く
appSharedWorkspace's openURLs:({ocidFilePathAURL}) withApplicationAtURL:(ocidAppPathURL) configuration:(ocidConfig) completionHandler:(missing value)

###ファイルB
set strFilePath to "/System/Library/Templates/Data/Library/User Pictures/Sports/Target.heic"
set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
set ocidFilePathBURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
###ファイルBを開く
appSharedWorkspace's openURLs:({ocidFilePathBURL}) withApplicationAtURL:(ocidAppPathURL) configuration:(ocidConfig) completionHandler:(missing value)




|

アプリケーションを『隠して』非表示で起動(考え中)

コマンドラインからだと起動しない なんで?=考え中

【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use framework "UniformTypeIdentifiers"
use scripting additions

property refMe : a reference to current application

###設定項目 フォントブック
property strBundleID : "com.apple.FontBook"
####################################
###アプリケーションのインストール先URLを取得
####################################
set appFileManager to refMe's NSFileManager's defaultManager()
set appNSWorkspace to refMe's NSWorkspace's sharedWorkspace()
##NSBundleで取得出来なければNSWorkspaceで取得
set ocidAppBundle to refMe's NSBundle's bundleWithIdentifier:(strBundleID)
if ocidAppBundle ≠ (missing value) then
  set ocidAppBundlePath to ocidAppBundle's bundlePath()
  set ocidAppPath to ocidAppBundlePath's stringByStandardizingPath
  set ocidAppPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidAppPath) isDirectory:false)
else if ocidAppBundle = (missing value) then
  set appNSWorkspace to refMe's NSWorkspace's sharedWorkspace()
  set ocidAppPathURL to appNSWorkspace's URLForApplicationWithBundleIdentifier:(strBundleID)
end if
####################################
###アプリケーションをHIDEで起動
####################################
set ocidFalse to (refMe's NSNumber's numberWithBool:false)
set ocidTrue to (refMe's NSNumber's numberWithBool:true)

set ocidOpenConfig to refMe's NSWorkspaceOpenConfiguration's configuration
ocidOpenConfig's setHides:(ocidTrue)
ocidOpenConfig's setActivates:(ocidFalse)
ocidOpenConfig's setRequiresUniversalLinks:(ocidFalse)
appNSWorkspace's openApplicationAtURL:(ocidAppPathURL) configuration:(ocidOpenConfig) completionHandler:(missing value)
(*
set ocidComponents to refMe's NSURLComponents's alloc()'s init()
ocidComponents's setScheme:("fontbook")
ocidComponents's setPath:("/")
set ocidAppSchemeURL to ocidComponents's |URL|
log ocidAppSchemeURL's absoluteString() as text

appNSWorkspace's openURLs:({ocidAppSchemeURL}) withApplicationAtURL:(ocidAppPathURL) configuration:(ocidOpenConfig) completionHandler:(missing value)
*)
####################################
###アプリケーションを隠す
####################################
set ocidRunningApplication to refMe's NSRunningApplication
set ocidAppArray to (ocidRunningApplication's runningApplicationsWithBundleIdentifier:(strBundleID))
repeat with itemAppArray in ocidAppArray
itemAppArray's hide()
end repeat


return "処理終了"


|

フォルダにアイコンを付与する

ダウンロード - フォルダにアイコン.zip

Icons_2

【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.6"
use framework "Foundation"
use framework "AppKit"
use scripting additions

property refMe : a reference to current application
###初期化
set appFileManager to refMe's NSFileManager's defaultManager()
set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
#####ダイアログを前面に
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if
###デスクトップ
set ocidDesktopPathArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidDesktopPathURL to ocidDesktopPathArray's firstObject()
set alisDesktopPath to (ocidDesktopPathURL's absoluteURL()) as alias
###ダイアログ
try
  set listAliasFolderPath to (choose folder "フォルダを選んでください" with prompt "フォルダを選択してください" default location alisDesktopPath with multiple selections allowed without invisibles and showing package contents) as list
on error
  log "エラーしました"
return
end try

#################################
###アイコンファイル名のレコード
set recordIcon to {|アクア|:"aqua.icns", |ブルー|:"blue.icns", |ダークブルー|:"darkblue.icns", |デフォルト|:"generic.icns", |グレー|:"gray.icns", |グリーン|:"green.icns", |オレンジ|:"orange.icns", |ピンク|:"pink.icns", |レッド|:"red.icns", |グラデーション|:"syft.icns", |バイオレット|:"violet.icns", |ホワイト|:"white.icns", |イエロー|:"yellow.icns"} as record
##可変レコード
set ocidIconDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
##レコードセット
ocidIconDict's setDictionary:(recordIcon)
##キーリスト
set ocidAllKeys to ocidIconDict's allKeys()
##ダイアログ用にリストに
set listAllKeys to ocidAllKeys as list
#####ダイアログを前面に
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if
###ダイアログ
try
  set listResponse to (choose from list listAllKeys with title "選んでください" with prompt "アイコンを選んでください" default items (item 8 of listAllKeys) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed)
on error
  log "エラーしました"
return "エラーしました"
end try
if listResponse is false then
return "キャンセルしました"
end if
set strResponse to (item 1 of listResponse) as text
###アイコンのファイル名
set ocidIconFileName to (ocidIconDict's valueForKey:(strResponse))
###パス
set aliasPathToMe to path to me as alias
set strPathToMe to POSIX path of aliasPathToMe as text
set ocidPathToMeStr to refMe's NSString's stringWithString:(strPathToMe)
set ocidPathToMe to ocidPathToMeStr's stringByStandardizingPath()
###コンテナディレクトリ
set ocidContainerDirPath to ocidPathToMe's stringByDeletingLastPathComponent
set ocidIconDirPath to ocidContainerDirPath's stringByAppendingPathComponent:"icns"
set ocidIconDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidIconDirPath) isDirectory:true)
###ICONパスURL
set ocidIconPathURL to ocidIconDirPathURL's URLByAppendingPathComponent:(ocidIconFileName) isDirectory:false
##アイコン用のイメージデータ取得
set ocidImageData to (refMe's NSImage's alloc()'s initWithContentsOfURL:(ocidIconPathURL))
########################
###本処理
########################
repeat with itemFolderPath in listAliasFolderPath
  ###フォルダパス
  set aliasFolderPath to itemFolderPath as alias
  set strFolderPath to POSIX path of aliasFolderPath as text
  set ocidFolderPathStr to (refMe's NSString's stringWithString:(strFolderPath))
  set ocidFolderPath to ocidFolderPathStr's stringByStandardizingPath()
  ###アイコン付与
  set boolAddIcon to (appSharedWorkspace's setIcon:(ocidImageData) forFile:(ocidFolderPath) options:(refMe's NSExclude10_4ElementsIconCreationOption))
  log (boolAddIcon & ":" & strFolderPath) as text
end repeat


|

[setDefaultApplicationAtURL]ファイルの関連付けを変更する


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
(*
com.cocolog-nifty.quicktimer.icefloe
*)
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions

property refMe : a reference to current application

###設定項目 ファイルタイプUTI
set strUTI to "com.netscape.javascript-source" as text


###UTTypに変換
set ocidUTType to refMe's UTType's typeWithIdentifier:(strUTI)
###ワークスペース初期化
set appShardWorkspace to refMe's NSWorkspace's sharedWorkspace()
###UTTypeタイプのデフォルトアプリケーション
set ocidAppPathURL to appShardWorkspace's URLsForApplicationsToOpenContentType:(ocidUTType)

################################################
###起動ボリュームにあるURLのみにする
###URL格納用の可変ARRAY
set ocidChooseArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0
ocidChooseArray's setArray:ocidAppPathURL
###収集したURLの個数
set numCntArray to count of ocidAppPathURL
###収集したURLから外部ボリュームのものを削除
###起動ボリュームの名前
set strFilePath to "/System/Library/CoreServices/Finder.app" as text
set ocidFinderFilePathStr to refMe's NSString's stringWithString:(strFilePath)
set ocidFinderFilePath to ocidFinderFilePathStr's stringByStandardizingPath()
set ocidFinderFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFinderFilePath) isDirectory:false)
set listVolumeNameKey to (ocidFinderFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLVolumeNameKey) |error|:(reference))
set strVolumeName to (item 2 of listVolumeNameKey) as text
####収集したURLの数だけ繰り返し
repeat numCntArray times
  ###Arrayの削除なので後ろから処理
  set itemAppPathURL to ocidChooseArray's objectAtIndex:(numCntArray - 1)
  ###ボリューム名を取得して
  set listPathVolumeName to (itemAppPathURL's getResourceValue:(reference) forKey:(refMe's NSURLVolumeNameKey) |error|:(reference))
  set strPathVolumeName to (item 2 of listPathVolumeName) as text
  ###外部ボリュームのものは削除
  if strPathVolumeName is "Preboot" then
    log strPathVolumeName
  else if strPathVolumeName is not strVolumeName then
ocidChooseArray's removeObjectAtIndex:(numCntArray - 1)
  end if
  set numCntArray to numCntArray - 1
end repeat


################################################
###アプリ名とURLのレコードを生成する
###ダイアログ用のアプリケーション名リスト
set listAppName to {} as list
###アプリケーションのURLを参照させるためのレコード
set ocidBrowserDictionary to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
####################################################
####UTTypeとして利用可能なアプリケーション一覧を取得する
####################################################
repeat with itemAppPathURL in ocidChooseArray
  ###アプリケーションの名前
  set listResponse to (itemAppPathURL's getResourceValue:(reference) forKey:(refMe's NSURLNameKey) |error|:(missing value))
  set strAppName to (item 2 of listResponse) as text
  log "ブラウザの名前は:" & strAppName & "です"
  copy strAppName to end of listAppName
  ####パス
  set aliasAppPath to itemAppPathURL's absoluteURL() as alias
  log "ブラウザのパスは:" & aliasAppPath & "です"
  ####バンドルID取得
  set ocidAppBunndle to (refMe's NSBundle's bundleWithURL:(itemAppPathURL))
  set ocidBunndleID to ocidAppBunndle's bundleIdentifier
  set strBundleID to ocidBunndleID as text
  log "ブラウザのBunndleIDは:" & strBundleID & "です"
(ocidBrowserDictionary's setObject:(itemAppPathURL) forKey:(strAppName))
end repeat

################################
##ダイアログ
################################
###ダイアログを前面に
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder"
    activate
  end tell
else
  tell current application
    activate
  end tell
end if
try
  set listResponse to (choose from list listAppName with title "選んでください" with prompt "URLを開くアプリケーションを選んでください" default items (item 1 of listAppName) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed)
on error
  log "エラーしました"
return "エラーしました"
end try
if listResponse is false then
return "キャンセルしました"
end if
set strResponse to (item 1 of listResponse) as text
################################
##アプリケーションのURLを取得する
################################
###アプリケーションのURLを取り出す
set ocidAppPathURL to ocidBrowserDictionary's objectForKey:(strResponse)
###選んだアプリケーションのバンドルID
set ocidAppBunndle to (refMe's NSBundle's bundleWithURL:(ocidAppPathURL))
set ocidBunndleID to ocidAppBunndle's bundleIdentifier
###IF用にテキストにしておく
set strBunndleID to ocidBunndleID as text


################################
##デフォルトに設定する
################################
set appShardWorkspace to refMe's NSWorkspace's sharedWorkspace()
appShardWorkspace's setDefaultApplicationAtURL:(ocidAppPathURL) toOpenContentType:(ocidUTType) completionHandler:(missing value)


################################
##設定後に 値の確定
################################

set strFilePath to "~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist"
set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
### PLISTを読み込む
set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s initWithContentsOfURL:(ocidFilePathURL)
###LSHandlers Array
set ocidLSHandlersArray to ocidPlistDict's objectForKey:"LSHandlers"
###項目数の数だけ繰り返し
repeat with itemLSHandlers in ocidLSHandlersArray
  ###コンテンツタイプを取得して
  set ocidLSHandlerContentType to (itemLSHandlers's valueForKey:"LSHandlerContentType")
  ###設定項目のコンテンツタイプと同じなら
  set strLSHandlerContentType to ocidLSHandlerContentType as text
  if strLSHandlerContentType is strUTI then
    ###変更前の値
    set ocidRoleViewer to (itemLSHandlers's valueForKey:"LSHandlerRoleViewer")
    set ocidRoleAll to (itemLSHandlers's valueForKey:"LSHandlerRoleAll")
    set ocidRoleAll to (itemLSHandlers's valueForKey:"LSHandlerRoleEditor")
    set ocidRoleShell to (itemLSHandlers's valueForKey:"LSHandlerRoleShell")
    log ocidRoleViewer as text
    log ocidRoleAll as text
    log ocidRoleAll as text
    log ocidRoleShell as text
    ####値を変更する
(itemLSHandlers's setValue:(strBunndleID) forKey:"LSHandlerRoleViewer")
(itemLSHandlers's setValue:(strBunndleID) forKey:"LSHandlerRoleAll")
(itemLSHandlers's setValue:(strBunndleID) forKey:"LSHandlerRoleEditor")
(itemLSHandlers's setValue:(strBunndleID) forKey:"LSHandlerRoleShell")
    ####変更後の値
    log (itemLSHandlers's valueForKey:"LSHandlerRoleViewer") as text
    log (itemLSHandlers's valueForKey:"LSHandlerRoleAll") as text
    log (itemLSHandlers's valueForKey:"LSHandlerRoleEditor") as text
    log (itemLSHandlers's valueForKey:"LSHandlerRoleShell") as text
  end if
  
end repeat
###保存
set boolDone to ocidPlistDict's writeToURL:(ocidFilePathURL) atomically:true
log boolDone as boolean


#############################
###CFPreferencesを再起動
#############################
#####CFPreferencesを再起動させて変更後の値をロードさせる
set strCommandText to "/usr/bin/killall cfprefsd" as text
do shell script strCommandText


#############################
###LaunchServicesを再起動
#############################
####lsregister初期化
(*#色々やってみたが不具合の方が多いので停止
set strCommandText to "/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -kill -seed" as text
do shell script strCommandText
*)


|

より以前の記事一覧

その他のカテゴリー

Accessibility Acrobat Acrobat 2020 Acrobat 2024 Acrobat AddOn Acrobat Annotation Acrobat AppleScript Acrobat ARMDC Acrobat AV2 Acrobat BookMark Acrobat Classic Acrobat DC Acrobat Dialog Acrobat Distiller Acrobat Form Acrobat GentechAI Acrobat JS Acrobat JS Word Search Acrobat Maintenance Acrobat Manifest Acrobat Menu Acrobat Merge Acrobat Open Acrobat PDFPage Acrobat Plugin Acrobat Preferences Acrobat Preflight Acrobat Print Acrobat Python Acrobat Reader Acrobat Reader Localized Acrobat Reference Acrobat Registered Products Acrobat SCA Acrobat SCA Updater Acrobat Sequ Acrobat Sign Acrobat Stamps Acrobat URL List Mac Acrobat URL List Windows Acrobat Watermark Acrobat Windows Acrobat Windows Reader Admin Admin Account Admin Apachectl Admin ConfigCode Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Mobileconfig Admin NetWork Admin Permission Admin Pkg Admin Power Management Admin Printer Admin Printer Basic Admin Printer Custompapers Admin SetUp Admin SMB Admin softwareupdate Admin Support Admin System Information Admin TCC Admin Tools Admin Umask Admin Users Admin Volumes Admin XProtect Adobe Adobe AUSST Adobe Bridge Adobe Documents Adobe FDKO Adobe Fonts Adobe Reference Adobe RemoteUpdateManager Adobe Sap Code AppKit Apple AppleScript AppleScript Duplicate AppleScript entire contents AppleScript List AppleScript ObjC AppleScript Osax AppleScript PDF AppleScript Pictures AppleScript record AppleScript Video Applications AppStore Archive Archive Keka Attributes Automator BackUp Barcode Barcode Decode Barcode QR Bash Basic Basic Path Bluetooth BOX Browser Calendar CD/DVD Choose Chrome Chromedriver CIImage CityCode CloudStorage Color Color NSColor Color NSColorList com.apple.LaunchServices.OpenWith Console Contacts CotEditor CURL current application Date&Time delimiters Desktop Desktop Position Device Diff Disk do shell script Dock Dock Launchpad DropBox Droplet eMail Encode % Encode Decode Encode HTML Entity Encode UTF8 Error EXIFData exiftool ffmpeg File File Name Finder Finder Window Firefox Folder FolderAction Font List FontCollections Fonts Fonts Asset_Font Fonts ATS Fonts Emoji Fonts Maintenance Fonts Morisawa Fonts Python Fonts Variable Foxit GIF github Guide HTML Icon Icon Assets.car Illustrator Image Events ImageOptim Input Dictionary iPhone iWork Javascript Jedit Ω Json Label Language Link locationd lsappinfo m3u8 Mail Map Math Media Media AVAsset Media AVconvert Media AVFoundation Media AVURLAsset Media Movie Media Music Memo Messages Microsoft Microsoft Edge Microsoft Excel Microsoft Fonts Microsoft Office Microsoft Office Link Microsoft OneDrive Microsoft Teams Mouse Music Node Notes NSArray NSArray Sort NSAttributedString NSBezierPath NSBitmapImageRep NSBundle NSCFBoolean NSCharacterSet NSData NSDecimalNumber NSDictionary NSError NSEvent NSFileAttributes NSFileManager NSFileManager enumeratorAtURL NSFont NSFontManager NSGraphicsContext NSGraphicsContext Crop NSImage NSIndex NSKeyedArchiver NSKeyedUnarchiver NSLocale NSMetadataItem NSMutableArray NSMutableDictionary NSMutableString NSNotFound NSNumber NSOpenPanel NSPasteboard NSpoint NSPredicate NSRange NSRect NSRegularExpression NSRunningApplication NSScreen NSSet NSSize NSString NSString stringByApplyingTransform NSStringCompareOptions NSTask NSTimeZone NSUbiquitous NSURL NSURL File NSURLBookmark NSURLComponents NSURLResourceKey NSURLSession NSUserDefaults NSUUID NSView NSWorkspace Numbers OAuth PDF PDF Image2PDF PDF MakePDF PDF nUP PDF Pymupdf PDF Pypdf PDFContext PDFDisplayBox PDFImageRep PDFKit PDFKit Annotation PDFKit AnnotationWidget PDFKit DocumentPermissions PDFKit OCR PDFKit Outline PDFKit Start PDFPage PDFPage Rotation PDFView perl Photoshop PlistBuddy pluginkit plutil postalcode PostScript PowerShell prefPane Preview Python Python eyed3 Python pip QuickLook QuickTime ReadMe Regular Expression Reminders ReName Repeat RTF Safari SaveFile ScreenCapture ScreenSaver Script Editor Script Menu SF Symbols character id SF Symbols Entity Shortcuts Shortcuts Events sips Skype Slack Sound Spotlight sqlite StandardAdditions StationSearch Subtitles LRC Subtitles SRT Subtitles VTT Swift swiftDialog System Events System Settings TemporaryItems Terminal Text Text CSV Text MD Text TSV TextEdit Tools Translate Trash Twitter Typography UI Unit Conversion UTType valueForKeyPath Video VisionKit Visual Studio Code VMware Fusion Wacom Weather webarchive webp Wifi Windows XML XML EPUB XML HTML XML LSSharedFileList XML LSSharedFileList sfl2 XML LSSharedFileList sfl3 XML objectsForXQuery XML OPML XML Plist XML Plist System Events XML RSS XML savedSearch XML SVG XML TTML XML webloc XML xmllint XML XMP YouTube Zero Padding zoom