メニュー
\ ポイント最大11倍! /詳細を見る

MacとWindowsのパスを相互に変換する方法│Automator

MAC使い始めて困ったのが、Windowsユーザーとのパスやり取りです。

変換サイトが一番確実なのですが、右クリックからパスの変換もできるように対応してみました。

  • Windowsのパス(テキスト)→ MACで開けるパスに変換
  • MACのファイルからパス取得 → Windowsで開けるパスに変換

海外でフリーランスをしているテクニカルアーティスト
ゲーム開発・映像制作・WEB制作等を請け負っています。
当ブログは作業効率化のための技術ブログです。

目次

Automatorの起動

MACで標準ではいっている「Automator」を使用します。

  1. Dock>Launchpad>その他>Automatorを開きます
  2. サービスを選択

あとは連携したい機能を選択して入力するだけです。

もらったWindowsの共有サーバーのパスをMACで開けるように変換する

Automatorで2つの設定をします。

STEP

シェルスクリプトを実行

「シェル/bin/bash」にして、「引数として」に。

あとの記述は下記を参照。

for f in "$@"
do
echo smb:${f//\\//}
done
STEP

クリップボードにコピー

Ctrl+Sで保存(Windows共有パスの変換.workflow)

以上でチャットなどで貼られるWindowsの共有サーバーのパスのテキストを選択して右クリック>サービス>Windows共有パスの変換でMACで読み込めるパスに変換されます。

Finderで選択ファイルからWindowsでも開けるパスに変換する

Automatorで3つの設定をします。

STEP

AppleScriptを実行

下記のコードを挿入します。

on splitString(str, delimiter)
    set OldDelims to AppleScript's text item delimiters
    set AppleScript's text item delimiters to delimiter
    set stringList to every text item of str
    set AppleScript's text item delimiters to OldDelims
    return stringList
end splitString

on getRealPath(input_file_path)
    set smbPrefix to "/Volumes/"
    if input_file_path starts with smbPrefix then
        set file_path to text items 2 thru -1 of splitString(input_file_path, smbPrefix) as string
        set mount_name to text item 1 of splitString(file_path, "/") as string
        set shell_command to "mount | grep \" on " & smbPrefix & mount_name & "\""
        set mount_path to do shell script shell_command
        set mount_path to text items 2 thru -1 of splitString(mount_path, "@") as string
        set mount_path to text item 1 of splitString(mount_path, " on /Volumes") as string
        set file_path to text items 2 thru -1 of splitString(file_path, mount_name & "/") as string
        return "smb://" & mount_path & "/" & file_path
    end if
    return input_file_path
end getRealPath

on searchReplace(theText, SearchString, ReplaceString)
    set OldDelims to AppleScript's text item delimiters
    set AppleScript's text item delimiters to SearchString
    set newText to text items of theText
    set AppleScript's text item delimiters to ReplaceString
    set newText to newText as text
    set AppleScript's text item delimiters to OldDelims
    return newText
end searchReplace

on toWindowsPath(file_path)
    set win_path to file_path
    set win_path to searchReplace(win_path, "<", "")
    set win_path to searchReplace(win_path, ">.", "")
    set win_path to searchReplace(win_path, ">", "")
    set win_path to searchReplace(win_path, "smb://", "\\\\")
    set win_path to searchReplace(win_path, "/", "\\")
    return win_path
end toWindowsPath

on run {input, parameters}
    set results to ""
    repeat with input_file in input
        set real_path to getRealPath(the POSIX path of input_file)
        if real_path starts with "smb://" then
            set results to results & toWindowsPath(real_path) & return
        else
            display dialog "ローカルパスをWindows用パスに変換できません" buttons {"OK"} default button "OK"
            return ""
        end if
    end repeat
    return results
end run
STEP

JavaScriptを実行

function run(input, parameters) {
    return decodeURI(input).normalize('NFC');
}
STEP

クリップボードにコピー

Ctrl+Sで保存(Windowsで開けるパスのコピー.workflow)

以上で、ファイルを選択>右クリック>クイックアクセス>Windowsで開けるパスのコピー.workflowからWindowsの人が開けるパスをコピーした状態で取得できます。

作成した「.workflow」の格納場所

ユーザー名>ライブラリ>Servicesの中にあります。

表示されてない時は、Finder>表示>表示オプションを表示>「“ライブラリ”フォルダを表示」にチェック

まとめ

とりあえずこれでパス問題はなんとかなりそうです。

もう少しmacに慣れたら一つのスクリプトで相互に変換できるようにしたいです。

全記事一覧

  • URLをコピーしました!

Windowsに関する参考記事・ツール・書籍のまとめ

Windowsのソフトウェア購入

Windowsの参考書籍

著:羽山 博, 著:吉川明広, 著:できるシリーズ編集部
¥2,673 (2024/04/15 22:28時点 | Amazon調べ)
\ポイント最大9倍!/
楽天市場
\ポイント5%還元!/
Yahooショッピング
富士通エフ・オー・エム
¥1,320 (2024/04/12 18:02時点 | Amazon調べ)
\ポイント最大9倍!/
楽天市場
\ポイント5%還元!/
Yahooショッピング
目次