Photoshop(beta)利用時に、リリース版にFinderの関連付けを変えるAppleScript

拡張子を指定
開くPhotoshopバージョンを指定

で、情報ウインドウでPhotoshopバージョンを指定して「全てを変更」します。

Bundle IDが同じためdutiでは変更できなかった。もっとマシな方法があれば教えてください………

2025/03/04 13:49 壁打ちして変更

```AppleScript
-- 初期拡張子設定
set baseExtList to {"psd", "psb"}
set extList to baseExtList

-- 拡張子表示用文字列
set extText to joinExt(extList)

-- 追加拡張子入力
try
	set inputExtList to paragraphs of text returned of (display dialog extText & " 以外の追加拡張子を改行で区切って入力" & return & "無い場合はキャンセルしてください。" & return & "空白行を入れないこと" default answer return)
on error number -128
	set inputExtList to {}
end try

-- 追加拡張子を統合
set extList to extList & inputExtList
set extText to joinExt(extList)


-- Photoshopアプリを検索・選択
set appList to paragraphs of (do shell script "mdfind \"kMDItemCFBundleIdentifier == 'com.adobe.Photoshop'\"")
if appList is {} then
	display dialog "Photoshop が見つかりませんでした。" buttons {"OK"} default button "OK"
	return
end if

if (count of appList) = 1 then
	set targetAppPath to item 1 of appList
else
	set chosenApp to choose from list appList with prompt "使用する Photoshop を選択"
	if chosenApp is false then
		display dialog "キャンセルされました。" buttons {"OK"} default button "OK"
		return
	end if
	set targetAppPath to item 1 of chosenApp
end if

tell application "System Events"
	set targetApp to name of disk item targetAppPath
end tell

-- 確認ダイアログ
display dialog extText & " のデフォルトアプリケーションを" & targetApp & "に変更します。" & return & "（！完了ダイアログが出るまでしばらく放置してください！）" buttons {"キャンセル", "OK"} default button "OK"
if button returned of result is "キャンセル" then return

-- デスクトップパス取得
set desktopFolder to path to desktop folder

-- 仮ファイル設定して作成
set baseFileName to "OpenWith2Photoshop"
set currentFileName to baseFileName
set baseFilePath to (desktopFolder as text) & baseFileName
do shell script "touch " & quoted form of (POSIX path of baseFilePath)

repeat with ext in extList
	set tempFileName to baseFileName & "." & ext
	
	tell application "Finder"
		-- 現在のファイルを取得
		set workFile to file currentFileName of desktopFolder
		
		-- 名前を変更
		set name of workFile to tempFileName
		delay 0.5
		set workFile to file tempFileName of desktopFolder
		
		open information window of workFile
		activate
	end tell
	
	delay 0.5
	my changeOpenWith(tempFileName, targetApp)
	delay 0.5
	
	tell application "Finder"
		close front window
	end tell
	
	-- 次のループ用に現在の名前を更新
	set currentFileName to tempFileName
end repeat

-- 最後に削除
tell application "Finder"
	delete file currentFileName of desktopFolder
end tell

display dialog extText & " のデフォルトアプリケーションを " & targetApp & " に変更しました。" buttons {"OK"} default button "OK"

-- カンマ区切り文字列化
on joinExt(extList)
	set AppleScript's text item delimiters to ","
	set extText to extList as text
	set AppleScript's text item delimiters to ""
	return extText
end joinExt

-- 「このアプリケーションで開く」の変更
on changeOpenWith(fileName, appName)
	set infoWindowName to fileName & "の情報"
	
	tell application "System Events"
		set infoWindow to window infoWindowName of application process "Finder"
		set operationArea to scroll area 1 of infoWindow
		set openWithSection to first UI element of operationArea whose value of attribute "AXIdentifier" is "openwith"
		
		if value of openWithSection is 0 then
			perform action "AXPress" of openWithSection
		end if
		
		try
			set popupButton to pop up button 1 of operationArea
		on error
			click UI element 5 of operationArea
		end try
		
		click popupButton
		
		try
			click menu item appName of menu 1 of popupButton
		on error
			key code 53
		end try
		
		delay 1
		
		set changeAllButton to first button of operationArea whose name is "すべてを変更…"
		if value of attribute "AXEnabled" of changeAllButton is true then
			perform action "AXPress" of changeAllButton
			delay 1
			tell application process "Finder"
				click button "続ける" of window 1
			end tell
		end if
	end tell
end changeOpenWith
```