Hiding and Unhiding Individual Files Using Applescript - applescript

I need to write an applescript to hide and unhide specific files. This is what I have so far, but apparently the spaces in the file name is causing problems.
tell application "System Events"
do shell script "chflags nohidden "/VOLUMES/Works/who/3130026 - Why Onspots.pdf""
end tell
Not sure what I'm doing wrong. Thanks in advance for the help.

You need to escape the quotes inside the shell command, using the backslash '\' character, like so
do shell script "chflags nohidden \"/VOLUMES/Works/who/3130026 - Why Onspots.pdf\""

Related

Passing Multiple Parameters from Applescript to Terminal Command Script

I have been trying to figure out how to pass multiple parameters from an Applescript to a Terminal Command Script. For example when running a terminal command file you are able to receive parameters programatically like so:
#!/bin/bash
var=$1
var=$2
The Applescript Code that I have been working with is below for reference:
tell application "System Events" to set app_directory to POSIX path of (container of (path to me))
set thisFile to "Dev"
set testTarget to "/Users/lab/Desktop/TestTarget/"
do shell script "/Users/lab/Desktop/TempRoot/mycommand.command " & thisFile & testTarget with administrator privileges
Where I think I have gone wrong is the input of the second parameter. When I only had one parameter it went through just fine:
do shell script "/path/to/command/mycommand.command" &var with administrative privileges
I am curious as to what the correct syntax would be for passing in this second parameter. If anybody has any suggestions please let me know! Also if you need more information I would be happy to provide it!
You just need to add a space between your arguments. Right now, there is no space being added between thisFile and testTarget. Your command looks like this:
/Users/lab/Desktop/TempRoot/mycommand.command Dev/Users/lab/Desktop/TestTarget/
Change your shell script line to:
do shell script "/Users/lab/Desktop/TempRoot/mycommand.command " & thisFile & space & testTarget with administrator privileges
Something that I find helpful when building a script is to make sure my shell commands are correct before running them. So instead of building it directly, store the command in a variable and log it. Later, replace the logging statement with the do shell script command.
set shellScript to "/Users/lab/Desktop/TempRoot/mycommand.command " & thisFile & space & testTarget with administrator privileges
log shellScript
-- do shell script shellScript

Terminal Applescript unable to escape quote

I've run into a strange problem when trying to include quote marks ' ' in my osascript command.
If I try and escape a normal escapable character, it works fine. Example: osascript -e 'tell app "Finder" to display dialog "Te\\st"' A dialog box from Finder pops up with the text Test in it.
However, the problem occurs when I try and use apostrophes when I'm writing out full sentences. Example: osascript -e 'tell app "Finder" to display dialog "Te\'st"' When I run this, all I'm left with is no dialog box, and the text input in terminal looking like this:
>
From what I know, this should by all means work, however, it doesn't.
Just to complement #Zero's helpful answer (which indeed does solve the problem):
Since you're using osascript, it is the shell's (bash's) quoting rules that apply first:
In bash (or any POSIX-compatible shell), you cannot include single quotes in a single-quoted string - not even with escaping.
What you CAN do, however, is to break your string into multiple pieces and simply splice in single quotes where needed (escaped outside a quoted string as \'):
osascript -e 'tell app "Finder" to display dialog "Te'\''st"'
'tell app "Finder" to display dialog "Te', the first part, is followed by the escaped single quote \', followed by the remainder of the string 'st"'
By virtue of having NO spaces between the parts, bash creates a single string that does contain the spliced-in literal '.
It is generally easier to pass single-quoted strings to osascript, since double quotes are frequently used in AppleScript and therefore have to be escaped when enclosed in a double-quoted string (as in the accepted answer).
In the typically infrequent event that you must pass a single quote to AppleScript, you can use the technique described in this answer.
You can do it this way:
osascript -e "tell app \"Finder\" to display dialog \"'Something' in quotes"\"

How can I use AppleScript to make Terminal.app run a shell script without evaluating the output as a script?

I'm using AppleScript to launch a quick-and-dirty shell script:
tell application "Terminal"
activate
do script "$(" & quoted form of MyScriptPath & ")"
end tell
Which properly launches a Terminal window and inputs what I would expect:
~$ $('/my script path/myscript.sh')
However, it seems that anything outputted to STDOUT (via echo) is evaluated as if it was inside the $( ) when evaluating/calling the script in the first place:
#!/bin/sh
echo "foobar"
produces:
-bash: foobar: command not found
I've searched far and wide and have not really found a suitable way to escape spaces in the path (rather than using "quoted form of") in AppleScript before sending the script location to Terminal, but I'd much prefer that. I'm using "do script" rather than "do shell script" because the script launching in Terminal is interactive and needs to be focused.
How can I echo to STDOUT when calling the script through $( )?
You don't need $(...) to run a command, only to include the output of that command in another string. You simply need
tell application "Terminal"
activate
do script "/my script path/myscript.sh"
end tell

How to change directory with spaces in applescript terminal?

I'm new to applescripts and I'm trying to automate a process, but how do you change directory through the script when there are spaces inside the directory? My commands should be correct but a syntax error keeps popping up:
Expected “"” but found unknown token.
Here is my script:
tell application "Terminal"
activate
do script "cd ~/Pictures/iPhoto\ Library"
end tell
I don't understand where it is wrong. It works fine on my terminal.
Thanks a bunch guys!!
UPDATE: this worked best!!
# surround in single quotes
tell application "Terminal"
activate
do script "cd '/Users/username/Pictures/iPhoto Library'"
end tell
The are a few ways.
# escape the quotes with a backslash. AND Escape the first backslash for Applescript to accept it.
tell application "Terminal"
activate
do script "cd ~/Pictures/iPhoto\\ Library"
end tell
# surround in double quotes and escape the quotes with a backslash.
tell application "Terminal"
activate
do script "cd \"/Users/username/Pictures/iPhoto Library\""
end tell
# surround in single quotes using quoted form of
tell application "Terminal"
activate
do script "cd " & quoted form of "/Users/username/Pictures/iPhoto Library"
end tell
# surround in single quotes
tell application "Terminal"
activate
do script "cd '/Users/username/Pictures/iPhoto Library'"
end tell
Also I do not thing the tild will expand when you use the quotes on the whole path.
So you will need to get the user name another way.
Examples:
# inserting the user name. And surrond in brackets so the name and path are seen as one string before the quotes are added
set whoami to do shell script "/usr/bin/whoami"
tell application "Terminal"
activate
do script "cd /Users/" & quoted form of whoami & "/Pictures/iPhoto\\ Library"
end tell
tell application "System Events" to set whoami to name of current user
# inserting the user name. And surrond in brackets so the name and path are seen as one string before the quotes are added
tell application "Terminal"
activate
do script "cd /Users/" & quoted form of (whoami & "/Pictures/iPhoto Library")
end tell
As you can see there is more than one way to do any of this.
Or just quote the directory part.
Example.
tell application "Terminal"
activate
do script "cd ~" & quoted form of "/Pictures/iPhoto Library"
end tell

How to pass a variable FROM applescript TO a shell script?

I have the following script
#!/bin/bash
/usr/bin/osascript << EOT
set myfile to choose file
EOT
no_ext=$(python -c "print '$myfile'.split('.')[0]")
### this works - just need to know how to pass the arg
R CMD Sweave no_ext.Rnw
pdflatex no_ext.tex
open no_ext.pdf
Can anyone point me to "how to pass the variable myfile correctly" ?
EDIT
Thx for all the suggestions!
Don't know what to accept, all of your answers really helped me since I learned a lot from everybody.
The following problems exist in your script:
A variable set in the AppleScript section does become defined in the enclosing shell script. You have to do the data exchange with the shell script by using command substitution.
AppleScripts invoked from a shell script aren't allowed to do user interaction because they do not have an application context. You can use the helper application "AppleScript Runner" to run user interaction commands.
Here is a revised version of your script where those problems are fixed:
#!/bin/bash
myfile=$(/usr/bin/osascript << EOT
tell app "AppleScript Runner"
activate
return posix path of (choose file)
end
EOT)
if [ $? -eq 0 ]
then
echo $myfile
else
echo "User canceled"
fi
First, you need to get the contents of the myfile variable from Applescript to bash. I don't know Applescript, so I'll make a shot in the dark as to how to write to its standard output. Then the python part is just unnecessary complexity (and likely wrong anyway, you were throwing away everything after the first . rather than the last). Next you need a $ before the variable name in bash syntax. I think the following script does what you want:
#!/bin/sh
set -e
myfile=$(osascript <<EOT
set myfile to choose file
write myfile to stdout
EOT
)
no_ext="${myfile%.*}"
R CMD Sweave "$no_ext.Rnw"
pdflatex "$no_ext.tex"
open "$no_ext.pdf"
(set -e at the beginning makes the shell exit immediately if an error occurs, instead of trying to execute pdflatex even though no .tex file has been produced or somesuch.)
Realize that applescript paths are colon ":" delimited. You need slash delimited in bash so in applescript terms that's the "posix path". Also, when using osascript it can't open dialog windows. You must tell an application to open the window. Next, you "return" something from the applescript... that's what goes to bash. Finally, in bash to execute a command and assign the result to a variable use `` around the command. So knowing this here's a shell script to use an applescript to get the myFile variable.
#!/bin/bash
myFile=`/usr/bin/osascript << EOT
tell application "Finder"
activate
set myfile to choose file with prompt "Select the file to use in bash!"
end tell
return (posix path of myfile)
EOT`
echo $myFile

Resources