简体   繁体   中英

Automator - dealing with spaces on paths that will be passed from Applescript to Shell Script

I have an AppleScript processing paths in the alias form and converting them to posix.

These paths will be processed by a shell script in the second step of this automator workflow.

The problem is that the path contains spaces.

I don't know how to deal with this, so I have this subroutine to remove any space and replace it with \\ .

on replaceChars(this_text, search_string, replacement_string)
    set saveTID to AppleScript's text item delimiters
    set AppleScript's text item delimiters to the search_string
    set the item_list to every text item of this_text
    set AppleScript's text item delimiters to the replacement_string
    set this_text to the item_list as string
    set AppleScript's text item delimiters to saveTID
    return this_text
end replaceChars

On the Applescript part I do

-- convert the path from the alias form to posix
set fileInputPosix to (the POSIX path of thePath)

-- replace " " with "\ "
set quotedForm to replaceChars(fileInputPosix, space, "\\ ")

at this point the paths appear to be ok, something like

'/Users/myUser/Desktop/my\ video\ file.png'

but when I pass this path to the shell script it will not accept that.

只需在外壳线中引用路径,然后删除搜索和替换部分

/usr/local/bin/ffmpeg -I "$1" -vf "select=eq(n\,$3)" -vframes 1 "$2"

In a very similar situation to your other question , you seem to be creating a problem that doesn't need solving by choosing to use an AppleScript action to receive file paths that ultimately end up being passed into a shell script action. It appears the only reason for doing this is to have the AppleScript "process" the file paths as alias file references, converting them into posix paths for the purpose of using them as command-line arguments in a shell script.

This is not at all necessary. Even if you decide an AppleScript is necessary for other reasons, Automator will often convert incompatible data types to an appropriate format when passing them between actions , so an AppleScript alias reference can be happily sent onto a shell script action, which will automagically receive them as posix file paths:

在Automator工作流程中如何处理文件路径

In the above screenshot, you can see that no processing or manipulation was performed by me at any stage, and both scripting actions return the correct file path one would expect for the language.

Conclusion

▸ Remove your AppleScript actions, and simply send the files directly through to the shell script, as arguments , and enclose your command-line argument variables in quotes as @vadian has already demonstrated.

▸ If you use an AppleScript for other reasons, that's fine. Just leave the alias references exactly as they are, and send them on afterwards to the shell script, and treat them the same way as just described.


File paths sent in the other direction—from a shell script to an AppleScript—won't, however, magically become alias references. A posix path is little more than a piece of text, and since AppleScript handles text perfectly well, there's no expectation on it to try to coerce it into something different; this would need to be done explicitly by the scripter, if necessary.

You can quote the whole string or use quoted form of to intelligently quote the string, and you can also escape individual characters as you are trying to do, but in that case you will need to escape each escape character, for example:

quoted form of “/Users/myUser/Desktop/my video file.png”
“/Users/myUser/Desktop/my\\ video\\ file.png”

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM