Windowsと違ってMacではファイルパスをコピペするだけでは該当ファイルにたどり着くことができず、いつもひとつひとつ階層をたどっていたので、ローカル環境のURLから現在ブラウザで表示しているファイルをワンクリックで開けるAppleScriptを作成しました。
HTMLを開いている時はエディタで該当ファイルが、画像を開いている時はFinderで該当ファイルが開きます。
指定する箇所は、下記の4箇所です。
- 使用ブラウザ(Google Chrome)
- 使用エディタ(Sublime Text)
- ローカル環境のURL(
https://kirakira.com:8080/) - ファイルを格納しているフォルダ名(/Users/username/Sites/kirakira.com/docs/)
tell application "Google Chrome"
set currentTab to active tab of front window
set currentURL to URL of currentTab
end tell
-- URLをローカルパスに変換
set localFilePath to convertToLocalPath(currentURL)
-- ファイルが存在するかチェック
set fileExists to (do shell script "test -e " & quoted form of localFilePath & " && echo true || echo false") as boolean
if not fileExists then
display dialog "File not found: " & localFilePath
return
end if
-- ファイルの拡張子を取得
set AppleScript's text item delimiters to "."
set fileExtension to last text item of localFilePath
set AppleScript's text item delimiters to ""
-- 画像かどうかで処理を分岐
if fileExtension is in {"jpg", "jpeg", "png", "svg", "webp"} then
-- Finderで開く
tell application "Finder"
set fileToReveal to POSIX file localFilePath as alias
reveal fileToReveal
activate
end tell
else
-- Sublime Textで開く
tell application "Sublime Text"
open localFilePath
activate
end tell
end if
-- URLをローカルパスに変換するハンドラ
on convertToLocalPath(theURL)
set AppleScript's text item delimiters to "/"
set urlParts to text items of theURL
set fileName to last item of urlParts
set urlDirectory to (text items 4 thru -2 of urlParts) as text
set localFilePath to ""
-- kirakira.com用のパス変換
if theURL starts with "https://kirakira.com:8080/" then
-- 拡張子をチェックして画像の場合はファイル名なしのパスを生成
if (fileName ends with ".jpg" or fileName ends with ".jpeg" or fileName ends with ".svg" or fileName ends with ".png" or fileName ends with ".webp") then
set localFilePath to "/Users/username/Sites/kirakira.com/docs/" & urlDirectory & "/" & fileName
else if (fileName ≠ "") then
set localFilePath to "/Users/username/Sites/kirakira.com/docs/" & urlDirectory & "/" & fileName
else
set localFilePath to "/Users/username/Sites/kirakira.com/docs/" & urlDirectory
end if
end if
-- pikapika.com用のパス変換
if theURL starts with "https://pikapika.com:8000/" then
-- 拡張子をチェックして画像の場合はファイル名なしのパスを生成
if (fileName ends with ".jpg" or fileName ends with ".jpeg" or fileName ends with ".svg" or fileName ends with ".png" or fileName ends with ".webp") then
set localFilePath to "/Users/username/Sites/pikapika.com/docs/" & urlDirectory & "/" & fileName
else if (fileName ≠ "") then
set localFilePath to "/Users/username/Sites/pikapika.com/docs/" & urlDirectory & "/" & fileName
else
set localFilePath to "/Users/username/Sites/pikapika.com/docs/" & urlDirectory
end if
end if
set AppleScript's text item delimiters to ""
return localFilePath
end convertToLocalPath