AppleScript NSWorkspace

iCloud Driveを開く


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

#!/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 appShardWorkspace to refMe's NSWorkspace's sharedWorkspace()

###################################
#####パス
###################################
set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSLibraryDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidLibraryDirPathURL to ocidURLsArray's firstObject()
###どっちが正解なのか?わからない
set ocidCloudStorageDirURL to ocidLibraryDirPathURL's URLByAppendingPathComponent:"Mobile Documents"
set ocidCloudStorageDirURL to ocidLibraryDirPathURL's URLByAppendingPathComponent:"Mobile Documents/com~apple~CloudDocs"

###################################
#####開く
###################################
set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
set ocidOpenURLsArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:0)
(ocidOpenURLsArray's addObject:(ocidCloudStorageDirURL))
appSharedWorkspace's activateFileViewerSelectingURLs:(ocidOpenURLsArray)

return
###この方法でもちろんOK
set aliasFilePathURL to (ocidCloudStorageDirURL's absoluteURL()) as alias
set boolResults to (appShardWorkspace's openURL:ocidCloudStorageDirURL)
if boolResults is false then
  tell application "Finder"
    make new Finder window to aliasFilePathURL
  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 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
*)


|

com.apple.launchservices.secureの書き換え


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
(*
com.cocolog-nifty.quicktimer.icefloe
applescript://
*)
----+----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

set strBunndleID to "XXXXXXXX.XXXXX.XXXXXXXX"

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")
    ####
(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
  
  
  log ocidRoleViewer as text
end repeat
###保存
set boolDone to ocidPlistDict's writeToURL:(ocidFilePathURL) atomically:true
log boolDone as boolean





|

NSURLのARRAYから外部ボリュームを削除する(途中)

もう少しスマートな方法がありそうな…

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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
(*
error number -128
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

###開くURL
set strOpenURL to "https://app.box.com" as text


###開くURLをNSURLに
set ocidOpenURL to refMe's NSURL's URLWithString:(strOpenURL)
###設定項目ドキュメントのURL
set strScheme to "https://" as text
###ワークスペース初期化
set appShardWorkspace to refMe's NSWorkspace's sharedWorkspace()
###NSURL
set ocidLocalHostURL to refMe's NSURL's URLWithString:(strScheme)
###URLタイプのデフォルトアプリケーション
set ocidAppPathURL to appShardWorkspace's URLsForApplicationsToOpenURL:(ocidLocalHostURL)
###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 ocidBrowserDictionary to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
###ダイアログ用のアプリケーション名リスト
set listAppName to {} as list

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


################################
##ダイアログ
################################
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)
###オプション
set ocidOpenConfiguration to refMe's NSWorkspaceOpenConfiguration's configuration()
ocidOpenConfiguration's setActivates:(true as boolean)
ocidOpenConfiguration's setHides:(false as boolean)
ocidOpenConfiguration's setRequiresUniversalLinks:(false as boolean)
ocidOpenConfiguration's setCreatesNewApplicationInstance:(true as boolean)
###開く
###openURLsはArrayで渡さないとクラッシュする
appShardWorkspace's openURLs:{ocidOpenURL} withApplicationAtURL:(ocidAppPathURL) configuration:(ocidOpenConfiguration) completionHandler:(missing value)

return




|

その他のカテゴリー

Acrobat Acrobat Annotation Acrobat Dialog Acrobat Form Acrobat JS Acrobat Open Acrobat Plugin Acrobat python Acrobat Reader Acrobat Sequ Acrobat Stamps Admin Admin Account Admin Apachectl Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Permission Admin Pkg Admin Power Management Admin Printer Admin SetUp Admin SMB Admin System Information Admin Tools Admin Users Admin Volumes Adobe Apple AppleScript AppleScript Guide AppleScript Accessibility AppleScript AppKit AppleScript Applications AppleScript AppStore AppleScript Archive AppleScript Attributes AppleScript Automator AppleScript AVAsset AppleScript AVconvert AppleScript AVFoundation AppleScript AVURLAsset AppleScript BackUp AppleScript Barcode AppleScript Bash AppleScript Basic AppleScript Basic Path AppleScript Bluetooth AppleScript BOX AppleScript Browser AppleScript Calendar AppleScript CD/DVD AppleScript Choose AppleScript Chrome AppleScript CIImage AppleScript CloudStorage AppleScript Color AppleScript com.apple.LaunchServices.OpenWith AppleScript Console AppleScript Contacts AppleScript CotEditor AppleScript CURL AppleScript current application AppleScript Date&Time AppleScript delimiters AppleScript Desktop AppleScript Device AppleScript Disk AppleScript do shell script AppleScript Dock AppleScript DropBox AppleScript eMail AppleScript Encode Decode AppleScript Error AppleScript EXIFData AppleScript ffmpeg AppleScript File AppleScript Finder AppleScript Firefox AppleScript Folder AppleScript Fonts AppleScript GIF AppleScript HTML AppleScript Icon AppleScript Illustrator AppleScript Image Events AppleScript Image2PDF AppleScript ImageOptim Applescript iWork AppleScript Javascript AppleScript Jedit AppleScript Json AppleScript Label AppleScript List AppleScript locationd AppleScript LRC AppleScript LSSharedFileList AppleScript m3u8 AppleScript MakePDF AppleScript Map AppleScript Math AppleScript Microsoft AppleScript Microsoft Edge AppleScript Microsoft Excel AppleScript Mobileconfig AppleScript Mouse AppleScript Movie AppleScript Music AppleScript NetWork AppleScript Notes AppleScript NSArray AppleScript NSBitmapImageRep AppleScript NSBundle AppleScript NSCFBoolean AppleScript NSCharacterSet AppleScript NSColor AppleScript NSColorList AppleScript NSData AppleScript NSDictionary AppleScript NSError AppleScript NSEvent AppleScript NSFileAttributes AppleScript NSFileManager AppleScript NSFont AppleScript NSFontManager AppleScript NSImage AppleScript NSIndex AppleScript NSKeyedArchiver AppleScript NSKeyedUnarchiver AppleScript NSLocale AppleScript NSMutableArray AppleScript NSMutableDictionary AppleScript NSMutableString AppleScript NSNotFound AppleScript NSNumber AppleScript NSOpenPanel AppleScript NSPasteboard AppleScript NSPDFImageRep AppleScript NSpoint AppleScript NSPredicate AppleScript NSRange AppleScript NSRegularExpression AppleScript NSRunningApplication AppleScript NSScreen AppleScript NSSize AppleScript NSString AppleScript NSStringCompareOptions AppleScript NSTask AppleScript NSTimeZone AppleScript NSURL AppleScript NSURL File AppleScript NSURLBookmark AppleScript NSURLComponents AppleScript NSURLResourceKey AppleScript NSUserDefaults AppleScript NSUUID AppleScript NSView AppleScript NSWorkspace AppleScript Numbers AppleScript OAuth AppleScript ObjC AppleScript OneDrive AppleScript Osax AppleScript PDF AppleScript PDFAnnotationWidget AppleScript PDFContext AppleScript PDFDisplayBox AppleScript PDFDocumentPermissions AppleScript PDFKit AppleScript PDFnUP AppleScript PDFOutline AppleScript Photos AppleScript Photoshop AppleScript Plist System Events AppleScript PostScript AppleScript prefPane AppleScript Preview AppleScript Python AppleScript QR AppleScript QR Decode AppleScript QuickLook AppleScript QuickTime AppleScript record AppleScript Regular Expression AppleScript Reminders AppleScript ReName AppleScript Repeat AppleScript RTF AppleScript Safari AppleScript SaveFile AppleScript ScreenCapture AppleScript ScreenSaver AppleScript Script Editor AppleScript Script Menu AppleScript Shortcuts AppleScript Shortcuts Events AppleScript Sort AppleScript Sound AppleScript Spotlight AppleScript SRT AppleScript StandardAdditions AppleScript stringByApplyingTransform AppleScript System Events AppleScript System Settings AppleScript TemporaryItems AppleScript Terminal AppleScript Text AppleScript Text CSV AppleScript Text TSV AppleScript TextEdit AppleScript Translate AppleScript Trash AppleScript Twitter AppleScript UI AppleScript Unit Conversion AppleScript UTType AppleScript valueForKeyPath AppleScript Video AppleScript VisionKit AppleScript Visual Studio Code AppleScript webarchive AppleScript webp AppleScript Wifi AppleScript XML AppleScript XML EPUB AppleScript XML OPML AppleScript XML Plist AppleScript XML RSS AppleScript XML SVG AppleScript XML TTML AppleScript XML webloc AppleScript YouTube AppleScript Zero Suppress Applications Barcode OnLine Tools CityCode Design iPhone List Logs lsappinfo Memo Music perl PlistBuddy pluginkit postalcode ReadMe SF Symbols character id SF Symbols Entity sips Skype Slack sqlite TCC Tools Typography Video Wacom zoom