使用 applescript 获取文件夹路径,在 .sh 文件中重用它

Get folder path with applescript reuse it in a .sh file

我正在尝试编写可供多个用户使用的工作流程脚本。用户需要指定一个 dropbox 文件夹的位置,该路径需要在 shell 和 applescript 中重复使用。

工作流的目标是自动设置监视文件更改的受监视文件夹(在 launchd 的帮助下)。当检测到更改时,应该触发一个 shell 脚本,该脚本 运行s: npm 运行 在监视文件夹的父级中生成。

我设法使用 .sh 文件和本地路径创建了工作流,但现在我想更新它,因此 运行 脚本的用户需要选择路径,因为这些路径每个用户不同。查看工作流程的步骤

  1. 选择保管箱文件夹位置
  2. 在该文件夹中创建可执行 shell 脚本文件
  3. 向该文件添加代码

3a:将使用所选文件夹路径的 applescript 添加到 运行 npm 命令
3b:为用户 LanchAgents 文件夹创建一个 launchd 文件(仅限 Mac 用户)
3c:使用所选文件夹的路径向启动文件添加内容

  1. 启动 launchd 文件。
    touch ~/Dropbox\ \(Folder\ name\)/folder/generate-icon-script.sh && echo "
    osascript -e 'tell application \"Terminal\"
            do script \"cd ~/Dropbox\\ \\(Folder\\ name\\)/folder/ && /usr/local/bin/npm run generate\"
            delay 10
            quit
        end tell'"  >> ~/Dropbox\ \(Folder\ name\)/folder/generate-icon-script.sh 
    && chmod a+x ~/Dropbox\ \(Folder\ name\)/folder/generate-icon-script.sh 
    && touch ~/Library/LaunchAgents/com.icons.daemon.plist 
    && echo "

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
    "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
        <dict>
            <key>Label</key>
            <string>com.icons.daemon.plist</string>
            <key>ProgramArguments</key>
            <array>
                <string>sh</string>
                <string>-c</string>
                <string>~/Dropbox\ \(Folder\ name\)//folder/generate-icon-script.sh</string>
            </array>
            <array>
                <string>~/Dropbox (Folder name)/folder/input</string>
            </array>
        </dict>
    </plist>
    " >> ~/Library/LaunchAgents/com.icons.daemon.plist &&
    launchctl load ~/Library/LaunchAgents/com.icons.daemon.plist

为了让用户确定路径,我创建了一个 applescript,提示用户 select 保管箱文件夹的正确位置。我设法在 dropbox 文件夹中创建了 .sh 文件,并向其中回显了一些内容。我面临的问题是我保存的路径不能在终端中使用。

set myLocation to (choose folder with prompt "Choose the location to the Icons Folder") # Get location to save the file
set myLocationPOSIX to the POSIX path of myLocation
do shell script "touch " & quoted form of myLocationPOSIX & "/test.sh"
do shell script "chmod a+x " & quoted form of myLocationPOSIX & "/test.sh"
do shell script "echo cd " & quoted form of myLocationPOSIX & " \&\& /usr/local/bin/npm run generate >> " & quoted form of myLocationPOSIX & "test.sh"

如何保存路径以便在我的工作流程中使用?任何帮助将不胜感激!

我希望回显的输出是: cd Users/name/Dropbox\ \(Folder\ name\ )/folder/ && /usr/local/bin/npm run generate

但我得到: cd /Users/name/Dropbox (Folder name)/folder/ && /usr/local/bin/npm run generate

任何帮助将不胜感激!

我认为 shell 你想要的是:

/Users/name/Dropbox\ (Folder\ name)/folder/

或者这个:

'/Users/name/Dropbox (Folder name)/folder/'

两者都可以正确解析。你现在得到的是后者,通过 quoted form of myLocationPOSIX。它应该可以工作,即使您没有看到预期的结果。使用文本项定界符可以获得前者,但这有点像 PITA:

set tid to text item delimiters
set text item delimiters to " "
set piecesList to text items of myLocationPOSIX
set text item delimiters to "\ "
set newLocationPath to piecesList as text
set text item delimiters to tid 

该代码在空格处断开位置路径,然后使用“\”再次将其连接在一起。完成了工作,但它并不漂亮...

是否有某些特定原因需要转义各个空格而不是引用整个路径?