Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

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

Reply
22 replies

Feb 1, 2016 2:59 PM in response to Niel

With a little test :

on run
  set str to "(\"/Users/conmeubonailleuco/Temporaire/converted/02.Knockers.mp3\")"
  set str to (items 2 thru -2 of str as string)

  return str
end run


It returns the quoted form :

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

So I really don't understand your message.


Can you help me ?


Thx.

Feb 1, 2016 3:19 PM in response to Combo

If the list only contains one string, use code such as:


set str to "(\"/Users/conmeubonailleuco/Temporaire/converted/02.Knockers.mp3\")"

set str to (items 3 thru -3 of str as string)

return str


For lists with multiple strings:


set str to paragraphs of "(

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

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

)"

set str_new to (item 1 of str) & return

repeat with this_paragraph from 2 to (count str) - 1

set str_new to str_new & (items 1 thru 2 of item this_paragraph of str as string) & (items 4 thru -2 of item this_paragraph of str as string) & return

if item -2 of str_new is "\"" then set str_new to (items 1 thru -3 of str_new as string) & return

end repeat

set str_new to str_new & item -1 of str


The previous line was designed to be used on individual strings, instead of a list of them.


(139078)

Feb 1, 2016 3:32 PM in response to Niel

A backslash remains in the result.


I could have one or multiple items in.


If I try your solution :

on run
  set str to "(\"/Users/conmeubonailleuco/Temporaire/converted/01.One Way Ticket.mp3\",\"/Users/conmeubonailleuco/Temporaire/converted/02.Knockers.mp3\")"
  set str to (items 3 thru -3 of str as string)

  return str
end run


The result is :

"/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"

)

Thx a lot.

Feb 1, 2016 3:49 PM in response to Combo

Not sure if we speak about the same thing but as the shell script return a list of file in a string (right ?), I have to put this result in a list object.


There is no simple AppleScript function to do that like "quoted form of POSIX path" to pass some files to a shell script ? I just want to reverse the "quoted form of POSIX path".


Thx a lot.

Feb 1, 2016 3:52 PM in response to Combo

For lists formatted the way in this thread, it's only easily removable for single-item lists. For AppleScript's own lists, it's also easily removable; a list of this type will look like:


{"/Users/conmeubonailleuco/Temporaire/converted/01.One Way Ticket.mp3", "/Users/conmeubonailleuco/Temporaire/converted/02.Knockers.mp3"}


(139081)

Feb 1, 2016 10:42 PM in response to Combo

Hello


I guess you're talking about the backslash + quote in result of an applescript like the following:


- applescript


do shell script "automator -i 'a 1.txt a 2.txt a 3.txt' ~/Desktop/a.workflow"



- ~/Desktop/a.workflow


Run Shell Script aciton

{

shell => /bin/bash,

pass input => as arguments,

code => as follows

}


for f in "$@" do echo "$f" done



- result (in result pane/window of (Apple)Script Editor)


"( \"a 1.txt\", \"a 2.txt\", \"a 3.txt\" )"




If it is the case, these backslashes along with the enclosing quotes at the beginning and the end are only in result pane/window of (Apple)Script Editor and actual resultant text printed by /usr/bin/automator is:


( "a 1.txt", "a 2.txt", "a 3.txt" )



However, this simply demonstrates how /usr/bin/automator is broken by design, which does not conform to standard unix command conventions with regards to passing arguments and printing outputs. Especially its output format is quite useless and cannot be used in pipe in normal means. Indeed the output should have been:


a 1.txt a 2.txt a 3.txt



and command invocation should have been:


/usr/bin/automator ~/Desktop/a.workflow 'a 1.txt' 'a 2.txt' 'a 3.txt'




---

Do you have any particular reason to use this flawed command via "do shell script" command in applescript? It seems to me very peculiar and overly complicated approach considering the fact that you're trying to use the output from /usr/bin/automator.


Regards,

H

Feb 2, 2016 4:37 PM in response to Hiroto

Hello and thank you for your answer,


I need to invoke an Automator script in AppleScript because I need a conditional structure. My process is splitted from my main process because it could be called from another process and it's more easier to maintain like that, like class in OOP. It's a process to encode some audio files, with lame in a shell script command and other stuff to format the files name, only if it's FLAC files or MP3 with bitrate greater than 192kbps. If not, I return the audio files passed in input and in my opinion, I can't use conditional structure directly in Automator, right ?


So to debug my process, I took a look in the output of a good action to pass the result to the action "Import Files into iTunes" in Automator which is for example :

(

"/Users/conmeubonailleuco/Temporaire/(2005) The Darkness - One Way Ticket To ****...And Back [FLAC] - copie/01.One Way Ticket.flac",

"/Users/conmeubonailleuco/Temporaire/(2005) The Darkness - One Way Ticket To ****...And Back [FLAC] - copie/02.Knockers.flac"

)

User uploaded file


But after that, the action return a wrong result and the rest of my process failed, like the result is a wrong format (not a list, right ?) :

"(

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

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

)"

User uploaded file

Do you what I mean ?


Thanks.

Feb 3, 2016 8:23 AM in response to Combo

Yes, you're correct. Automator is a vehicle without steering. I would avoid automator in the first place rather than wasting time in clearing up its mess...


As for the useless output from /usr/bin/automator, you can neither use it as input to next shell command nor next automator action without clearing those stupid parentheses, quotes, commas and indent spaces.


Here're some options:


1) Provided that the external automator workflow you're invoking by /usr/bin/automator via "do shell script" command consists of a Run Shell Script action, invoke the shell script in the action directly by "do shell script" command in AppleScript. In other words, write everything in AppleScript or shell script without resorting to some poor Automator facility.


E.g., Given that shell script in a Run Shell Script action is as follows:



#!/bin/bash for f in "$@" do echo "$f" done




and a workflow ~/Desktop/a.workflow consisting of the single action is invoked by /usr/bin/automator as follows:



do shell script "/usr/bin/automator -i 'file 1 file 2 file 3' ~/Desktop/a.workflow"




then replace it with the following:



do shell script "/bin/bash -s <<'EOF' - 'file 1' 'file 2' 'file 3' for f in \"$@\" do echo \"$f\" done EOF"




which will return:



file 1 file 2 file 3




and you may convert it into AppleScript list by getting the pargraphs of it.




2) Bother yourself to clear up the output's mess of /usr/bin/automator.


E.g., Given the same assumptions in 1), use this:



do shell script "/usr/bin/automator -i 'file 1 file 2 file 3' ~/Desktop/a.workflow | sed -En 's/\"?,?$//; s/[[:space:]]+\"?(.*)$/\\1/p'"




which will return:



file 1 file 2 file 3




* Note that the sed(1) code in 2) only works with workflow with Run Shell Script action because Run AppleScript action yields output in different format when run by /usr/bin/automator. Also in 2), you cannot handle filename containing linefeed character due to the limitation of /usr/bin/automator whereas you can handle it in 1) by adapting shell script and AppleScript so as to use \0 for output line separator.



Observed and tested under OS X 10.6.8. Later versions may possibly vary.


Good luck,

H

Feb 2, 2016 6:48 PM in response to Combo

Addendum.


You mentioned modularity. So here's an alternative version of previous option 1).



1a) Save the shell script in the Run Shell Script action as separate file (as a modular command) and invoke it by do shell script command.


E.g., Given the same assumptions in 1), save the shell script:



#!/bin/bash for f in "$@" do echo "$f" done




in ~/Desktop/a.sh and invoke it by the following:



do shell script "~/Desktop/a.sh 'file 1' 'file 2' 'file 3'" result's paragraphs




which will return AppleScript list:



{"file 1", "file 2", "file 3"}




Regards,

H

Feb 3, 2016 8:44 AM in response to Hiroto

Hi,


I tried to test your solution 2 to clear up the output's mess of /usr/bin/automator with sed. But not sure to understand it well...

Here is my AppleScript code :

set theOptions to " | sed -En 's/\"?,?$//; s/[[:space:]]+\"?(.*)$/\\1/p"
  set theWorkflow to quoted form of ("/Users/conmeubonailleuco/Library/Mobile Documents/com~apple~Automator/Documents/Encode Audio Files.workflow" & theOptions)

  set theAutomatorScript to "automator -i " & thePath & " " & theWorkflow
  do shell script theAutomatorScript


I have an error :

The workflow file does not exist.


Same in Terminal with :

automator -i "01.One Way Ticket.flac" "/Users/conmeubonailleuco/Library/Mobile\ Documents/com\~apple\~Automator/Documents/Encode\ Audio\ Files.workflow | sed -En 's/\"?,?$//; s/[[:space:]]+\"?(.*)$/\\1/p"


It seems I can't use the sed option after the script's path. Am I wrong ?


Thx.

Feb 3, 2016 7:47 PM in response to Combo

Hello


You get the said error because you're passing the whole (theWorkflow & theOptions) as the workflow argument to /usr/bin/automator. Also your sed code is missing the trailing apostrophe.


Correction would be as follows. I have changed some variable names and added code to build -i argument of /usr/bin/automator.



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 thePostprocessor to " | sed -En 's/\"?,?$//; s/[[:space:]]+\"?(.*)$/\\1/p'" set theAutomatorScript to "automator -i " & theArgs's quoted form & " " & theWorkflow's quoted form do shell script theAutomatorScript & thePostprocessor




Regards,

H

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

Remove the quoted form of POSIX path

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.