Page 1 of 1

Escaping Spaces in Shell Scripts

PostPosted: Fri Jan 11, 2013 5:20 pm
by Bryan
I am having the craziest time. I am trying to do a find shell script and have hit an issue where the posix path contains a space, and the find is failing because of that.

The offending portion of the script:

Code: Select all
tell application "Finder"
   set posix_parent_dir to POSIX path of (container of (item theFile) as text)
end tell

set showFolders to ((do shell script "find posix_parent_dir -maxdepth 1 -mindepth 1 -type d -name " * " -print") as text)


I have run the find with the same path in Terminal and it works fine. HELP!!!

Re: Escaping Spaces in Shell Scripts

PostPosted: Sat Jan 12, 2013 1:06 pm
by a_freyer
Quoted Form is what you're looking for:

Code: Select all
tell application "Finder"
   set posix_parent_dir to quoted form of the POSIX path of (container of (item theFile) as text)
end tell

set showFolders to ((do shell script "find posix_parent_dir -maxdepth 1 -mindepth 1 -type d -name " * " -print") as text)

Re: Escaping Spaces in Shell Scripts

PostPosted: Sun Jan 13, 2013 3:19 pm
by Bryan
Thanks! That got me the posix path correctly. Now I am getting an error message that:

Code: Select all
2013-01-13 13:02:51.141 hazelworker[54964] AppleScript error: {
    NSLocalizedDescription = "find: posix_parent_dir: No such file or directory";
    NSLocalizedFailureReason = "find: posix_parent_dir: No such file or directory";
    OSAScriptErrorAppAddressKey = "<NSAppleEventDescriptor: [0x0,2 \"hazelworker\"]>";
    OSAScriptErrorAppNameKey = hazelworker;
    OSAScriptErrorBriefMessageKey = "find: posix_parent_dir: No such file or directory";
    OSAScriptErrorMessageKey = "find: posix_parent_dir: No such file or directory";
    OSAScriptErrorNumberKey = 1;
    OSAScriptErrorRangeKey = "NSRange: {0, 0}";


with the following script:

Code: Select all
tell application "Finder"
   set posix_parent_dir to the quoted form of the POSIX path of (container of (item theFile) as text)
end tell

set showFolders to ((do shell script "find posix_parent_dir -maxdepth 1 -mindepth 1 -type d -name \"*\" -print") as text)


I have tested it in Terminal and it works fine, so somehow it is not translating over to Applescript in Hazel. Not that it should matter, but the folder is on another internal hard drive (not the startup disk) whose path is "/Volumes/Snow Leprd/Data/Clients/" When I put the command in on terminal, I have to escape the space between Snow and Leprd, or I get the same error leading me to believe this is my problem. The only other change from my original script is that I escaped the quotation marks around the asterisk. This is why I hate shell scripts...

Thanks,
Bryan

Re: Escaping Spaces in Shell Scripts

PostPosted: Sun Jan 13, 2013 4:19 pm
by a_freyer
Lets back up, what are you trying to do here? Can you post the entire AppleScript?

Re: Escaping Spaces in Shell Scripts

PostPosted: Sun Jan 13, 2013 4:29 pm
by Bryan
The rest of the applescript should seem oddly familiar to you. :D Per your request:

Code: Select all
tell application "Finder"
   set myHazelTokenDelimiters to "|"
   set theListOfCustomTokens to name of theFile
   set AppleScript's text item delimiters to {myHazelTokenDelimiters}
   -- Now, the tokens are available in theListOfCustomTokens as items 1,2,3 etc...
   
   -- The following is an example
   set Name1 to (text item 1 of theListOfCustomTokens)
   set Name2 to (text item 2 of theListOfCustomTokens)
   set Name3 to (text item 3 of theListOfCustomTokens)
end tell

tell application "Finder"
   set posix_parent_dir to the quoted form of the POSIX path of (container of (item theFile) as text)
end tell

-- to make sure the correct directory is chosen for debugging only
tell application "System Events"
   activate
   display dialog posix_parent_dir
end tell


set showFolders to ((do shell script "find posix_parent_dir -maxdepth 1 -mindepth 1 -type d -name \"*\" -print") as text)

-- to make sure the find worked for debugging only
tell application "System Events"
   activate
   display dialog showFolders
end tell



Eventually I am going to match up the correct subfolder for the file's keywords (the subfolders are named with the pattern of [client lastname], [client firstname] [(optional) client middle initial], [matter type] [date of incident]. I can't use Hazel's sort into subfolders because I can't make an identical match. Further, one client may have multiple client matters, and so I want a bit more intelligence in the sorting. Lastly, I do not want a new folder made if the filing fails, I will just move the pdf to a generic folder and sort it by hand. Obviously, I haven't gotten into the sorting logic yet with the script.

Thanks,
Bryan

Re: Escaping Spaces in Shell Scripts

PostPosted: Mon Jan 14, 2013 10:25 pm
by Bryan
After a day's perspective I decided to try a bit of brute force to escape my space. I also added a try block to capture the thrown error resulting in this code:

Code: Select all
tell application "Finder"
   set tid to AppleScript's text item delimiters
   set myHazelTokenDelimiters to "|"
   set theListOfCustomTokens to name of theFile
   set AppleScript's text item delimiters to {myHazelTokenDelimiters}
   -- Now, the tokens are available in theListOfCustomTokens as items 1,2,3 etc...
   
   -- The following is an example
   set Name1 to (text item 1 of theListOfCustomTokens)
   set Name2 to (text item 2 of theListOfCustomTokens)
   set Name3 to (text item 3 of theListOfCustomTokens)
end tell

tell application "Finder"
   set posix_parent_dir to the quoted form of the POSIX path of (container of (item theFile) as text)
end tell

set AppleScript's text item delimiters to space

set escapedSpace to ""

if ((the number of text items of posix_parent_dir) > 1) then
   repeat with counter from 1 to ((the number of text items of posix_parent_dir) - 1)
      set escapedSpace to escapedSpace & (text item counter of posix_parent_dir & (ASCII character 92)) & space
   end repeat
   set escapedSpace to escapedSpace & (the last text item of posix_parent_dir)
   set posix_parent_dir to escapedSpace
end if

tell application "System Events"
   activate
   display dialog posix_parent_dir
end tell

try
   set showFolders to ((do shell script "find posix_parent_dir -maxdepth 1 -mindepth 1 -type d -name \"*\" -print") as text)
   
   tell application "System Events"
      activate
      display dialog showFolders
   end tell
on error
   tell application "System Events"
      activate
      display dialog "Find Failed"
   end tell
end try


Of course, the find is still failing, even though the space is now escaped. posix_parent_dir is exactly '/Volumes/Snow\ Leprd/Data/Clients/' which, if I plug it into the find command in terminal works perfectly. I have also tried this as the find line:

Code: Select all
set showFolders to ((do shell script "find posix_parent_dir -maxdepth 1 -mindepth 1 -type d -name " & (ASCII character 34) & "*" & (ASCII character 34) & " -print") as text)


so I wouldn't have to escape the quotes in the line. Lastly I even tried this:
Code: Select all
set showFolders to ((do shell script "find posix_parent_dir -maxdepth 1 -mindepth 1 -type d -name " & (ASCII character 34) & (ASCII character 42) & (ASCII character 34) & " -print") as text)

ASCII 34 is "
ASCII 42 is *


None of this is working. I would love some other thoughts on this. Thanks in advance.

Bryan

Re: Escaping Spaces in Shell Scripts

PostPosted: Mon Jan 14, 2013 10:29 pm
by a_freyer
[EDIT - see below]

Re: Escaping Spaces in Shell Scripts

PostPosted: Mon Jan 14, 2013 10:33 pm
by a_freyer
Oh, damn. I'm sorry I should have noticed this before. You need to concatenate the text of the shell script and your posix path variable:

Code: Select all
"find " &posix path &"rest of the script"


So it ends up like this

Code: Select all
set showFolders to ((do shell script "find" &  posix_parent_dir & " -maxdepth 1 -mindepth 1 -type d -name \"*\" -print") as text)


AppleScript does not expand quoted variables.

Re: Escaping Spaces in Shell Scripts

PostPosted: Tue Jan 15, 2013 12:05 am
by Bryan
Thanks for the help! I modified the script. It is now:

Code: Select all
tell application "Finder"
   set tid to AppleScript's text item delimiters
   set myHazelTokenDelimiters to "|"
   set theListOfCustomTokens to name of theFile
   set AppleScript's text item delimiters to {myHazelTokenDelimiters}
   -- Now, the tokens are available in theListOfCustomTokens as items 1,2,3 etc...
   
   -- The following is an example
   set Name1 to (text item 1 of theListOfCustomTokens)
   set Name2 to (text item 2 of theListOfCustomTokens)
   set Name3 to (text item 3 of theListOfCustomTokens)
end tell

tell application "Finder"
   set posix_parent_dir to the quoted form of the POSIX path of (container of (item theFile) as text)
end tell

set AppleScript's text item delimiters to space

set escapedSpace to ""

if ((the number of text items of posix_parent_dir) > 1) then
   repeat with counter from 1 to ((the number of text items of posix_parent_dir) - 1)
      set escapedSpace to escapedSpace & (text item counter of posix_parent_dir & (ASCII character 92)) & space
   end repeat
   set escapedSpace to escapedSpace & (the last text item of posix_parent_dir)
   set posix_parent_dir to escapedSpace as text
end if

tell application "System Events"
   activate
   display dialog posix_parent_dir
end tell

try
   set showFolders to ((do shell script "find " & posix_parent_dir & " -maxdepth 1 -mindepth 1 -type d -name \"*\" -print") as text)
   
   tell application "System Events"
      activate
      display dialog showFolders
   end tell
on error
   tell application "System Events"
      activate
      display dialog "Find Failed"
   end tell
end try


and it still doesn't work. In a word, AAAAAARRRRRRRGGGGGHHHHH!!!!!!!!!!!! Also, I think you meant to put a space after the find and before the " in the second code section. You put it in the first section, but not the second. (See, I am starting to pick up something here, but I still hate Shell Scripts)

My concern is that when I escape the space in the path name I am screwing something up. Thanks again!

Bryan

p.s. As I was writing this, it suddenly dawned on me. I am using this on a certain folder only. It will ALWAYS be at the path '/Volumes/Snow Leprd/Data/Clients/' so I changed the find to this:

Code: Select all
tell application "Finder"
   set tid to AppleScript's text item delimiters
   set myHazelTokenDelimiters to "|"
   set theListOfCustomTokens to name of theFile
   set AppleScript's text item delimiters to {myHazelTokenDelimiters}
   -- Now, the tokens are available in theListOfCustomTokens as items 1,2,3 etc...
   
   -- The following is an example
   set Name1 to (text item 1 of theListOfCustomTokens)
   set Name2 to (text item 2 of theListOfCustomTokens)
   set Name3 to (text item 3 of theListOfCustomTokens)
end tell


try
   set showFolders to ((do shell script "find /Volumes/Snow\\ Leprd/Data/Clients/ -maxdepth 1 -mindepth 1 -type d -name \"*\" -print") as text)
   
   tell application "System Events"
      activate
      display dialog showFolders
   end tell
on error
   tell application "System Events"
      activate
      display dialog "Find Failed"
   end tell
end try


with the path hardwired in it works. It isn't so portable, but I am not planning to move it to any other folder for the moment. Thanks for helping me through this, and if someone comes up with the answer, I would love to hear it.

Bryan