You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mac转Windows路径自动化:无法传递选中文本至Python脚本

MacOS Automator服务实现Mac路径转Windows路径问题解决

问题概述

迁移到MacOS后,团队常用Windows的D盘映射网络路径,需要通过Automator右键服务快速将选中的Mac路径转为Windows路径,但当前脚本无法正确接收选中的文本,输出固定为"Input"。

问题原因

Automator服务传递选中文本的方式与终端运行脚本不同,原脚本通过sys.argv读取命令行参数,但Automator未正确传递选中内容,反而传入了占位符字符串。

解决方案

方案1:通过标准输入读取选中文本(推荐)

1. 修正Python脚本

将脚本改为读取标准输入(Automator默认将选中文本传入标准输入):

# Convert MacOS Path to Windows Path
import sys

def mtw(link):
    # 替换Mac路径前缀为Windows的D盘,同时转换路径分隔符
    w_path = link.replace("/Users/FF/Server", "D:\\").replace("/", "\\")
    print(w_path)

if __name__ == '__main__':
    # 读取标准输入的选中文本,去除首尾空白
    input_text = sys.stdin.read().strip()
    if input_text:
        mtw(input_text)

2. 配置Automator快速操作

  • 打开Automator,新建快速操作(原"服务")
  • 顶部设置:
    • 工作流程收到:选定的文本
    • 位于:任何应用程序
  • 添加运行Shell脚本动作:
    • Shell选择/bin/bash
    • 传递输入选择到标准输入
    • 输入命令:/usr/bin/python3 /绝对路径/到你的脚本.py(替换为实际脚本路径)

方案2:通过命令行参数传递文本

1. 修正Python脚本

改为读取第一个命令行参数:

# Convert MacOS Path to Windows Path
import sys

def mtw(link):
    w_path = link.replace("/Users/FF/Server", "D:\\").replace("/", "\\")
    print(w_path)

if __name__ == '__main__':
    # 检查是否有传入参数
    if len(sys.argv) > 1:
        mtw(sys.argv[1])

2. 配置Automator快速操作

  • 同方案1的前两步,在运行Shell脚本动作中:
    • 传递输入选择作为自变量
    • 输入命令:/usr/bin/python3 /绝对路径/到你的脚本.py "$@"

测试验证

选中任意Mac格式路径(如/Users/FF/Server/docs/file.txt),右键→快速操作→你的服务名称,文本会自动替换为D:\docs\file.txt。

内容的提问来源于stack exchange,提问作者Francisco Chaves

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 21:36:00