syntax error: Expected end of line but found unknown token - terminal

I am attempting to run some AppleScript in the terminal, just to learn how to do it, and I am running into some trouble with the & sign. Even when the & is replaced with the "&", it still returns the same error.. I have narrowed it down to the problem with the & sign, and am wondering if anyone has any advice about it. I am getting the following error:
syntax error: Expected expression but found unknown token.
My code that I type into the terminal:
osascript -e 'tell application "Finder"' -e 'set location to (path to home folder as string) & \"testing.plist\"' -e 'if (exists file location) then' -e 'say location' -e 'end if' -e 'end tell'

You don't need the backslashes to escape the double quotes for "testing.plist", removing those will fix that error.
Additionally, the word "location" is used by the Finder dictionary, so you'll want to use some other term for that variable, such as "myLocation" instead.

Related

Run 2 commands in applescript/osascript with variable

I am trying to run 2 commands stored in a variable with osascript
This is my start.sh
currentDirectory="cd $(pwd) && npm run start"
echo $currentDirectory
osascript -e 'tell application "Terminal" to do script '"${currentDirectory}"''
I am getting this as the output
sh start.sh
cd /Users/Picadillo/Movies/my-test-tepo && npm run start
83:84: syntax error: Expected expression but found “&”. (-2741)
#Barmar: The argument to do script needs to be in double quotes.
Yes; however, the way you’ve done it is still unsafe.
If the path itself contains backslashes or double quotes, AS will throw a syntax error as the munged AS code string fails to compile. (One might even construct a malicious file path to execute arbitrary AS.) While these are not characters that frequently appear in file paths, best safe than sorry. Quoting string literals correctly is always a nightmare; correctly quoting them all the way through shell and AppleScript quadratically so.
Fortunately, there is an easy way to do it:
currentDirectory="$(pwd)"
osascript - "${currentDirectory}" <<EOF
on run {currentDirectory}
tell application "Terminal"
do script "cd " & (quoted form of currentDirectory) & " && npm run start"
end tell
end run
EOF
Pass the currentDirectory path as an additional argument to osascript (the - separates any options flags from extra args) and osascript will pass the extra argument strings as parameters to the AppleScript’s run handler. To single-quote that AppleScript string to pass back to shell, simply get its quoted form property.
Bonus: scripts written this way are cleaner and easier to read too, so less chance of overlooking any quoting bugs you have in your shell code.
The argument to do script needs to be in double quotes.
osascript -e 'tell application "Terminal" to do script "'"${currentDirectory}"'"'
You should also put the argument to cd in quotes, in case it contains spaces.
currentDirectory="cd '$(pwd)' && npm run start"

Can't pass variable with spaces in name through a bash osascript shell script

I'm writing a script to pass a complete file path with spaces in the name to a remote computer over ssh, which needs to run using iTerm. It needs to happen through AppleScript embedded into a shell script so I can take control of iTerm, create new split views, etc.
I've simplified my script here purely to the section that isn't working. For demonstration purposes, and so people here might be able to try alternatives, I've changed the command in question to the 'cd' command, as the error is the same (it can't find the path due to spaces in it).
#!/bin/bash
PATH="$1"
ssh server#192.168.50.4 "osascript \
-e 'tell application \"iTerm\"' \
-e 'activate' \
-e 'set newWindow to (create window with default profile)' \
-e 'tell first session of current tab of current window' \
-e 'write text \"cd "$PATH"\"' \
-e 'end tell' \
-e 'end tell'"
This works fine if there's no spaces in the path. The remote machine opens up an iTerm window and the path is changed to cd path. However, if there's spaces in the name, such as…
Volumes/Work/My Folder With Spaces
…iTerm responds with -bash: cd: Volumes/Work/My: No such file or directory
I've tried using $# and putting extra quotes around the $1 variable and/or the $PATH variable. Also, I know there's an AppleScript command that's something like "quoted form of (POSIX path)" but I have no idea how to integrate it here within the bash script.
I just figured it out! the script I was writing was an After Effects render script. Turns out it became a lot simpler if I used EOF instead of starting each line with -e. It solved all the issues I was having with nested quotes, etc.
#!/bin/bash
ssh server#192.168.50.4 osascript <<EOF
tell application "iTerm"
activate
set newWindow to (create window with default profile)
select first window
tell current session of current window
end tell
tell first session of current tab of current window
write text "/Applications/Adobe\\\\ After\\\\ Effects\\\\ 2020/aerender -project \"$1\" -sound ON"
end tell
end tell
EOF

How to deal with white spaces in osascript [duplicate]

This question already has answers here:
Combining variables in Bash to form a command sent to AppleScript using the osascript command
(2 answers)
Closed 5 years ago.
I'm in need of your assistance on how to deal with white space in a path.
In my example below, I'm setting some variables in a Terminal bash shell and executing, "osascript" from command line, utilizing these variables to run ['do shell script'] & ['display dialog']. When I run these from command line, I'm getting different results. My ultimate goal here is to be able to execute ['do shell script'] and it execute the bash shell script based on the path. In this case, the path has spaces in it.
NOTE: The INSTALLER_PATH variable defined below is set like this because the path is generated from an Apple Script I wrote that basically takes a path [ with white spaces in it ] and combines that path with another variable. Because this path has spaces in the name, I'm using the [ to quoted form of ] setting that puts the path in quotes.
APPLE SCRIPT EXAMPLE:
set pathToApp to POSIX path of ((path to me) as text)
set dragonFrame to ("_DRAGONFRAME/")
set INSTALLER_PATH to quoted form of pathToApp & dragonFrame
display dialog INSTALLER_PATH
GENERATES THIS PATH {notice the ticks}:
'/Volumes/Free.Space/Shotgun Python Dragon Project 2017/DRAGONFRAME_SCRIPTS_MASTER_V1.02/Dragonframe_Scripts_Installer.app/'_DRAGONFRAME/
Instead of testing this through the Apple Script to find the exact syntax I would need, I figured it would be easier to test this from command line. This is exactly what I'm setting and executing from my Terminal command line. In the examples below, executing [ 'display dialog' ] works and [ 'do shell script' ] fails. I think if there's a way to enclose the entire path in double quotation marks, my problem would be solved although all attempts at getting the path enclosed in double quotation marks has failed:
%> INSTALLER_PATH='/Volumes/Free.Space/Shotgun Python Dragon Project 2017/DRAGONFRAME_SCRIPTS_MASTER_V1.02/Dragonframe_Scripts_Installer.app/'_DRAGONFRAME/
%> ADMIN_USER_PROC="_Python_PySide_QT_Installer/Scripts/AdminUserProcesses.sh"
%> osascript -e 'do shell script ("'"${INSTALLER_PATH}"'" & "'"${ADMIN_USER_PROC}"'")'
GENERATES THIS ERROR:
0:217: execution error: sh: /Volumes/Free.Space/Shotgun: No such file or directory (127)
%> osascript -e 'display dialog ("'"${INSTALLER_PATH}"'" & "'"${ADMIN_USER_PROC}"'")'
GENERATES THIS PATH:
/Volumes/Free.Space/Shotgun Python Dragon Project 2017/DRAGONFRAME_SCRIPTS_MASTER_V1.02/Dragonframe_Scripts_Installer.app/_DRAGONFRAME/_Python_PySide_QT_Installer/Scripts/AdminUserProcesses.sh
Thank you in advance for any help you can offer.
I think you're looking for something similar to this syntax:
set dragonFrame to ("/_DRAGONFRAME/")
set INSTALLER_PATH to path of (pathToApp & dragonFrame)
Result:
'/Volumes/Free.Space/Shotgun Python Dragon Project 2017/DRAGONFRAME_SCRIPTS_MASTER_V1.02/Dragonframe_Scripts_Installer.app/_DRAGONFRAME/'
If you need double-quotes around the path you can use:
("\"" & pathToApp & dragonFrame & "\"")
and get rid of the quoted form argument leaving you with:
set INSTALLER_PATH to ("\"" & pathToApp & dragonFrame & "\"")

Combining variables in Bash to form a command sent to AppleScript using the osascript command

I'm trying to make this script work. It's a Bash script that is meant to take some variables, put them together and use the result to send an AppleScript command. Manually pasting the string echoed from the variable to_osa behind osascript -e to the terminal works as I want and expect it to. But when I try to combine the command osascript -e and the string to_osa, it does not work. How can I make this work?
the_url="\"http://stackoverflow.com/questions/1521462/looping-through-the-content-of-a-file-in-bash\""
the_script='tell application "Safari" to set the URL of the front document to '
delimiter="'"
to_osa=${delimiter}${the_script}${the_url}${delimiter}
echo ${to_osa}
osascript -e ${to_osa}
In addition to working manually the script also works when I write the desired command to a script and then execute it:
echo "osascript -e" $to_osa > ~/Desktop/outputfile.sh
sh ~/Desktop/outputfile.sh
String mashing executable code is error prone and evil, and there's absolutely no need for it here. It's trivial to pass arguments to an AppleScript by defining an explicit 'run' handler:
on run argv -- argv is a list of strings
-- do stuff here
end run
which you then invoke like so:
osascript -e /path/to/script arg1 arg2 ...
BTW, if your script requires a fixed number of args, you also write it like this:
on run {arg1, arg2, ...} -- each arg is a string
-- do stuff here
end run
...
Going further, you can even make the AppleScript directly executable as you would any other shell script. First, add a hashbang as follows:
#!/usr/bin/osascript
on run argv
-- do stuff here
end run
then save it in uncompiled plain text format and run chmod +x /path/to/myscript to make the file executable. You can then execute it from the shell as follows:
/path/to/myscript arg1 arg2 ...
Or, if you don't want to specify the full path every time, put the file in /usr/local/bin or some other directory that's on your shell's PATH:
myscript arg1 arg2 ...
...
So here's how you should be writing your original script:
#!/bin/sh
the_url="http://stackoverflow.com/questions/1521462/looping-through-the-content-of-a-file-in-bash"
osascript -e 'on run {theURL}' -e 'tell application "Safari" to set URL of document 1 to theURL' -e 'end run' $the_url
Quick, simple, and very robust.
--
p.s. If you'd rather open a URL in a new window rather than an existing one, see the manpage for OS X's open tool.
As a general rule, don't put double-quotes in the variable, put them around the variable. In this case it's more complicated, since you have some double-quotes for bash-level quoting, and some for AppleScript-level quoting; in this case, the AppleScript-level quotes go in the variable, the bash-level quotes go around the variable:
the_url="\"http://stackoverflow.com/questions/1521462/looping-through-the-content-of-a-file-in-bash\""
the_script='tell application "Safari" to set the URL of the front document to '
osascript -e "${the_script}${the_url}"
BTW, using echo to check things like this is highly misleading. echo is telling you what's in the variable, not what'll be executed when you reference the variable on a command line. The biggest difference is that echo prints its arguments after they've been through bash parsing (quote and escape removal, etc), but when you say "Manually pasting the string ... works" you're saying it's what you want before parsing. If the quotes are there in the echoed string, that means bash didn't recognize them as quotes and remove them. Compare:
string='"quoted string"'
echo $string # prints the string with double-quotes around it because bash doesnt't recognize them in a variable
echo "quoted string" # prints *without* quotes because bash recognizes and removes them

Why doesn't this simple AppleScript work?

I have a Shell Script where I need to create aliases folders on a MacOSX 10.6.X so I call osascript to do it with the code below:
Source="/Volumes/Test Project/Folder/SubFolder"
Destination="/Volumes/Test Project/Dest/"
/usr/bin/osascript -e 'tell application "Finder" to make alias file to POSIX file "$Source" at POSIX file "$Destination"'
This code returns:
29:103: execution error: Finder got an error: AppleEvent handler failed. (-10000)
Does anyone have a solution?
The shell doesn't substitute variables (e.g. $Source) inside single-quoted strings (e.g. the entire AppleScript command). Solution: use double-quotes around the command (which means you need to escape the double-quotes inside it with backslashes).
/usr/bin/osascript -e "tell application \"Finder\" to make alias file to POSIX file \"$Source\" at POSIX file \"$Destination\""
Any reason keeping you from using: ln -s "$Source" "$Destination"?

Categories

Resources