Help with shell script syntax using osacompile and heredoc

I’ve got a batch script to find and replace text in lots of applescript files.

The .applescript files were easy using Applescript’s “read” and “write” commands, but I’m using osadecompile and osacompile to handle the .scpt files (and .scpt files inside app bundles.)

Decompiling went smoothly, but I can’t get compiling to work.

To test my script, first save this script somewhere:

display dialog "Change This"

Then run this script and select the saved first script when prompted.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set theScript to choose file with prompt "Please select a script file to process."

set scriptPOSIXpath to quoted form of POSIX path of theScript

set scriptText to do shell script ("osadecompile " & scriptPOSIXpath)

set revisedText to replace_chars(scriptText, "Change This", "Look, it changed!")

set shellCompileCommand to "osacompile -o " & scriptPOSIXpath & " << \"END_TEXT\"" & return & revisedText & return & "END_TEXT"

do shell script shellCompileCommand

-- set recompiledScript to do shell script quoted form of revisedText & " | osacompile"


-- standard find and replace
on replace_chars(theText, searchString, replacementString)
	set AppleScript's text item delimiters to the searchString
	set the item_list to every text item of theText
	set AppleScript's text item delimiters to the replacementString
	set theText to the item_list as string
	set AppleScript's text item delimiters to ""
	return theText
end replace_chars

The error I get is:

If I take the contents of my variable “shellCompileCommand” and paste them in terminal, it works. I’m having trouble figuring out what’s going wrong trying to run the exact same thing with “do shell script.” It seems to be having some sort of issue with (or a command near?) the open parenthesis, but I don’t understand why.

Thanks in advance for any help,

t.spoon.

Nevermind.

I was trying to think what on earth could be changing between running it from Applescript and pasting in the exact same text to the terminal.

Then it hit me - clipboard/shell might be fixing linefeed character choice on paste.

This version works.


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set theScript to choose file with prompt "Please select a script file to process."

set scriptPOSIXpath to quoted form of POSIX path of theScript

set scriptText to do shell script ("osadecompile " & scriptPOSIXpath)

set revisedText to replace_chars(scriptText, "Change This", "Look, it changed!")

set shellCompileCommand to "osacompile -o " & scriptPOSIXpath & " << \"END_TEXT\"" & linefeed & revisedText & linefeed & "END_TEXT"

do shell script shellCompileCommand

-- set recompiledScript to do shell script quoted form of revisedText & " | osacompile"


-- standard find and replace
on replace_chars(theText, searchString, replacementString)
	set AppleScript's text item delimiters to the searchString
	set the item_list to every text item of theText
	set AppleScript's text item delimiters to the replacementString
	set theText to the item_list as string
	set AppleScript's text item delimiters to ""
	return theText
end replace_chars

Just a change from using “return” to “linefeed.”

This is why my time estimates on scripting projects are always overly optimistic.