0

I have a simple AppleScript that calls a perl shell script to replace a string with another string (here one with two) in files with specified file extensions (here .txt and .xyz).

It used to work exactly as I have it below, but no longer does (possibly since updating to Mojave last year).

set CleanFiles to "
    s/one/two/g;
    "

set myFolder to choose folder with prompt "Choose a folder with files to be cleaned up:"
set theFolder to POSIX path of myFolder

do shell script "find " & theFolder & " \\( -name \\*.txt -o -name \\*.xyz \\) -print0 | xargs -0 perl -i -pe '" & CleanFiles & "'"

There is no error message, it seems to run but doesn't do anything.

What could be wrong?

5
  • Well, the obvious: you name the variable CleanFiles in the first line, but use CleanElanFiles in the last line. is that the problem, or just a transcription error? – Ted Wrigley Feb 4 '20 at 18:19
  • @TedWrigley ooops, sorry no that was a typo – the actual thing is much more complex but I've stripped it down as a minimal example here – jan Feb 4 '20 at 18:21
  • Ah. Ok, then I think your problem is that you should quote the folder path line. change line 3 to read set theFolder to quoted form of (POSIX path of myFolder). When I do that on my test folder your script seems to work. – Ted Wrigley Feb 4 '20 at 18:39
  • @TedWrigley Awesome, yes!! That was it. Thanks a ton! I guess I can never figure out when to use the quoted form and when not ... :P Anyhow, if you turn your comment into an answer I'll checkmark it. – jan Feb 4 '20 at 18:43
  • 1
    Use the quoted form of whenever you think there's a chance that whitespace might sneak into a variable. Just remember that unix uses space as a delimiter, and that if an unquoted space gets tossed in you'll turn what should be one argument into two, and throw off the entire argument list. I'll add an answer... – Ted Wrigley Feb 4 '20 at 18:52
1

The issue is that you need to quote file paths for unix, using the quoted form of command in AppleScript. In other words, line three should read:

set theFolder to quoted form of (POSIX path of myFolder)

The problem happens because Mac variables can legally contain special characters — like spaces and single quotes — that the unix system uses. So, if you were to choose a folder named "My Test Folder", AppleScript would return its posix path as /Users/username/My Test Folder, but the command line invoked by do shell script would read that as three separate arguments, not one single file path. The quoted form of command quotes and escapes any unix special characters in the line of text so that they are not interpreted as special characters by the shell.

6
  • @user3439894 Spaces are special characters to the shell: they are argument delimiters. if I write ls -al /~/username the spaces signify that 'ls' is the command, and that '-al' and '/~/username' are arguments passed to that command. Mess up the spaces — e.g., type ls -al /~/user name and the command won't work as expected. Also, quoted form of escapes single-quotes with backslashes. in other words quoted form of "'bob'" will output "''\\''bob'\\'''" – Ted Wrigley Feb 4 '20 at 19:32
  • @user3439894 — Hmph. that output is even weirder than I thought... – Ted Wrigley Feb 4 '20 at 19:33
  • Yeah, my bad, when I was checking special characters in the bash manual I was looking for symbols and for metacharacter I saw "‘|’, ‘&’, ‘;’, ‘(’, ‘)’, ‘<’, or ‘>’" but missed the part where it said "A metacharacter is a space, tab, newline, or one of the following characters:" That said though, ls -al /~/username returns: ls: /~/username: No such file or directory and ls -al /~/user name returns similar. – user3439894 Feb 4 '20 at 22:38
  • @user3439894 — that ls thing is just me sloppily goofing up unix. I should have written ls -al ~/<folder name> to indicate that you should use some actual folder in your home directory with a space in it's name. Then the difference between ls -al ~/<folder name> and ls -al '~/<folder name>' becomes clear. I should really take the time and review things before I submit posts <sigh...>. – Ted Wrigley Feb 4 '20 at 23:37
  • Only because you said "I should really take the time and review things before I submit posts <sigh...>' ... you do realize that tilde expansion doesn't take place when quoted, single or double. :) Not to worry tough, we all at times post things we should have first reviewed. :) – user3439894 Feb 5 '20 at 0:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .