Combo

Q: Remove the quoted form of POSIX path

Hi,

 

I use the quoted form of POSIX path in a AppleScript script to pass files in arguments to a shell script (an automator process (see man automator)).

 

When the process is finished, i'd like to pass the result to another action but the string is quoted like that :

"(

  \"/Users/conmeubonailleuco/Temporaire/converted/01.One Way Ticket.mp3\",

  \"/Users/conmeubonailleuco/Temporaire/converted/02.Knockers.mp3\"

)"

Instead of :

(

  "/Users/conmeubonailleuco/Temporaire/converted/01.One Way Ticket.mp3",

  "/Users/conmeubonailleuco/Temporaire/converted/02.Knockers.mp3"

)

What is the function to use to do that ?

 

Thx a lot.

Posted on Feb 1, 2016 2:14 PM

Close

Q: Remove the quoted form of POSIX path

  • All replies
  • Helpful answers

Previous Page 2
  • by Hiroto,Helpful

    Hiroto Hiroto Feb 4, 2016 9:49 AM in response to Hiroto
    Level 5 (7,286 points)
    Feb 4, 2016 9:49 AM in response to Hiroto

    I have revised sed code so as to handle escaped quote and escaped backslash in output of /usr/bin/automator correctly.

     

    You may replace:

     

     

    set thePostprocessor to " | sed -En 's/\"?,?$//; s/[[:space:]]+\"?(.*)$/\\1/p'"
    

     

     

     

    with:

     

     

    set thePostprocessor to " | sed -En 's/\"?,?$//; s/[\\]\"/\"/g; s/[\\]{2}/\\\\/g; s/[[:space:]]+\"?(.*)$/\\1/p'"
    

     

     

     

    Regards,

    H

  • by Combo,

    Combo Combo Feb 4, 2016 10:08 AM in response to Hiroto
    Level 1 (8 points)
    Apple TV
    Feb 4, 2016 10:08 AM in response to Hiroto

    Hi and thank you for your answer.

     

    It helps me a lot but after testing your solution, it seems I still have an error below (sorry for my OS in french ) when the result is passed to the next action ("Import Files into iTunes") of my process :

    1.png

    I think it's because the action is waiting for a list and not a string so I have to format the differents file's paths in a list, right ? See the panel result below :

    2.png

    Instead of a good format in for example the "Get Selected Finder Items" automator action which works :

    3.png

     

    I'm looking for that...

     

    Thx.

  • by Hiroto,Solvedanswer

    Hiroto Hiroto Feb 4, 2016 10:36 AM in response to Combo
    Level 5 (7,286 points)
    Feb 4, 2016 10:36 AM in response to Combo

    In AppleScript, you need to get paragraphs of output from shell script. Like this.

     

     

    set theFiles to {"/path/to/file1.flac", "/path/to/file2.flac"}
    set theArgs to ""
    repeat with f in theFiles
        set theArgs to theArgs & f & linefeed
    end repeat
    if theArgs ends with linefeed then set theArgs to theArgs's text 1 thru -2
    -- theArgs = "/path/to/file1.flac" & linefeed & "/path/to/file2.flac"
    
    set theWorkflow to "/Users/conmeubonailleuco/Library/Mobile Documents/com~apple~Automator/Documents/Encode Audio Files.workflow"
    set theAutomatorScript to "automator -i " & theArgs's quoted form & " " & theWorkflow's quoted form
    set thePostprocessor to " | sed -En 's/\"?,?$//; s/[\\]\"/\"/g; s/[\\]{2}/\\\\/g; s/[[:space:]]+\"?(.*)$/\\1/p'"
    do shell script theAutomatorScript & thePostprocessor
    return result's paragraphs
    

     

     

     

    H

  • by Hiroto,

    Hiroto Hiroto Feb 4, 2016 10:47 AM in response to Combo
    Level 5 (7,286 points)
    Feb 4, 2016 10:47 AM in response to Combo

    Or possibly you may have to convert the file path to POSIX file object. Like this.

     

     

    set theArgs to ""
    repeat with f in theFiles
        set theArgs to theArgs & f & linefeed
    end repeat
    if theArgs ends with linefeed then set theArgs to theArgs's text 1 thru -2
    -- theArgs = "/path/to/file1.flac" & linefeed & "/path/to/file2.flac"
    
    set theWorkflow to "/Users/conmeubonailleuco/Library/Mobile Documents/com~apple~Automator/Documents/Encode Audio Files.workflow"
    set theAutomatorScript to "automator -i " & theArgs's quoted form & " " & theWorkflow's quoted form
    set thePostprocessor to " | sed -En 's/\"?,?$//; s/[\\]\"/\"/g; s/[\\]{2}/\\\\/g; s/[[:space:]]+\"?(.*)$/\\1/p'"
    do shell script theAutomatorScript & thePostprocessor
    set rr to result's paragraphs
    repeat with r in rr
        set r's contents to r as POSIX file
    end repeat
    return rr
    

     

     

     

    I'm really fed up with tedious Automator...

     

    H

  • by Combo,

    Combo Combo Feb 4, 2016 10:51 AM in response to Hiroto
    Level 1 (8 points)
    Apple TV
    Feb 4, 2016 10:51 AM in response to Hiroto

    Yes I knew but my knowledge in AppleScript makes me not enough confortable to use the good function...

     

    It works like a charm with "result's paragraphs" so thank you very much !

  • by Combo,

    Combo Combo Feb 4, 2016 6:57 PM in response to Combo
    Level 1 (8 points)
    Apple TV
    Feb 4, 2016 6:57 PM in response to Combo

    Hi,

     

    As it works with files selected in the Finder, I tried to do the same with a sound files folder, "Get Folder Contents" action and "Filter Finder Items" action to get only the MP3 and FLAC files to pass to my process, and here is the new bug... My process fails with no error and nothing is returned.

     

    If I log my automator script, it is like that :

    automator -i 'Macintosh HD:Users:conmeubonailleuco:Temporaire:The Clash - London Calling [Mp3 320 Kbps] - copie:01 The Clash London Calling.mp3

    Macintosh HD:Users:conmeubonailleuco:Temporaire:The Clash - London Calling [Mp3 320 Kbps] - copie:02 The Clash Brand New Cadillac.mp3

    Macintosh HD:Users:conmeubonailleuco:Temporaire:The Clash - London Calling [Mp3 320 Kbps] - copie:03 The Clash Jimmy Jazz.mp3' '/Users/conmeubonailleuco/Library/Mobile Documents/com~apple~Automator/Documents/Encode Audio Files.workflow'

    I see anything wrong...

     

    Any idea ?

     

    Thx.

  • by Hiroto,

    Hiroto Hiroto Feb 4, 2016 9:22 PM in response to Combo
    Level 5 (7,286 points)
    Feb 4, 2016 9:22 PM in response to Combo

    Hello

     

    You're using HFS path where POSIX path is required.

     

    E.g., You need to use :

     

    /Users/conmeubonailleuco/Temporaire/The Clash - London Calling [Mp3 320 Kbps] - copie/01 The Clash London Calling.mp3
    

     

     

    in lieu of :

     

    Macintosh HD:Users:conmeubonailleuco:Temporaire:The Clash - London Calling [Mp3 320 Kbps] - copie:01 The Clash London Calling.mp3
    

     

     

     

    In Automator's Run AppleScript action, the input argument holds a list of values returned by the previous action. If the previous action is something returning file system objects, the input is usually a list of AppleScript's aliases. And when an AppleScript's alias is coerced to string, the result is HFS path of the alias, which cannot be used in shell script. In order to get POSIX path from an AppleScript's alias, you need to use "POSIX path" property of alias.

     

    So the code in Run AppleScript action would be something like this:

     

     

    on run {input, parameters}
        set theFiles to input
        set theArgs to ""
        repeat with f in theFiles
            set theArgs to theArgs & f's POSIX path & linefeed -- # get POSIX path of f
        end repeat
        if theArgs ends with linefeed then set theArgs to theArgs's text 1 thru -2
        
        set theWorkflow to "/Users/conmeubonailleuco/Library/Mobile Documents/com~apple~Automator/Documents/Encode Audio Files.workflow"
        set theAutomatorScript to "automator -i " & theArgs's quoted form & " " & theWorkflow's quoted form
        set thePostprocessor to " | sed -En 's/\"?,?$//; s/[\\]\"/\"/g; s/[\\]{2}/\\\\/g; s/[[:space:]]+\"?(.*)$/\\1/p'"
        do shell script theAutomatorScript & thePostprocessor
        return result's paragraphs
    end run
    

     

     

     

    Good luck,

    H

  • by Hiroto,

    Hiroto Hiroto Feb 4, 2016 9:33 PM in response to Hiroto
    Level 5 (7,286 points)
    Feb 4, 2016 9:33 PM in response to Hiroto

    PS. And if you are to follow conventions and have Run AppleScript action return alias list to Automator workflow, use something like this:

     

    on run {input, parameters}
        set theFiles to input -- list of aliases
        set theArgs to ""
        repeat with f in theFiles
            set theArgs to theArgs & f's POSIX path & linefeed -- string of POSIX paths joined by linefeed
        end repeat
        if theArgs ends with linefeed then set theArgs to theArgs's text 1 thru -2
        
        set theWorkflow to "/Users/conmeubonailleuco/Library/Mobile Documents/com~apple~Automator/Documents/Encode Audio Files.workflow"
        set theAutomatorScript to "automator -i " & theArgs's quoted form & " " & theWorkflow's quoted form
        set thePostprocessor to " | sed -En 's/\"?,?$//; s/[\\]\"/\"/g; s/[\\]{2}/\\\\/g; s/[[:space:]]+\"?(.*)$/\\1/p'"
        do shell script theAutomatorScript & thePostprocessor
        set rr to result's paragraphs -- list of POSIX paths
        repeat with r in rr
            set r's contents to r as POSIX file as alias
        end repeat
        return rr -- list of aliases
    end run
    

     

     

    H

Previous Page 2