Updated Applescript to hide folders on Desktop for El Capitan - applescript

I would like to hide all folders on Desktop. I found the following AppleScript but somehow it doesn't work under el Capitan (I'm running 10.11.5 version) :
try
set toggle to do shell script "defaults read com.apple.finder CreateDesktop"
if toggle = "true" then
do shell script "defaults write com.apple.finder CreateDesktop false"
else if toggle = "false" then
do shell script "defaults write com.apple.finder CreateDesktop true"
end if
end try
do shell script "killall Finder"
delay 0.5
activate application "Finder"
Thanks in advance for help

Your found AppleScript has some issues.
The value of the key CreateDesktop – like the more popular AppleShowAllFiles – is a boolean.
Boolean values of user defaults return "1" or "0" (string) in a do shell script call for a couple of system versions. It's never "true" or "false". To get an usable result you need to coerce it to integer and then to boolean
((do shell script "/usr/bin/defaults read com.apple.finder CreateDesktop") as integer) as boolean
If the key does not exist (the default value is false) catch the thrown error, then toggle the boolean state and write is back with -bool attribute to ensure not to write a string.
After sending killall the Finder relaunches automatically, there's no need to do that in code.
This script can be used also for the AppleShowAllFiles key to show and hide invisible files.
try
set state to ((do shell script "/usr/bin/defaults read com.apple.finder CreateDesktop") as integer) as boolean
on error
set state to false
end try
do shell script "/usr/bin/defaults write com.apple.finder CreateDesktop -bool " & ((not state) as text) & "; killall Finder"

Related

Passing data into Automator Shell

How would I receive input using "Ask For Text" in MacOS Automator and then pass that data into part of a shell script?
i.e.
[Ask for text/folder path]
[set inputted text to variable "data"]
[run shell script:
chflags hidden (CALL "DATA" HERE)]
I would like the above example to yield the specified folder hidden.
The Run Shell Script action has a "Pass input" pop-up menu in its upper right that lets you select how you want the input from the previous action to be passed to the script. The options are "to stdin" or "as arguments".
For your case, "as arguments" is probably easiest to handle.
Then, you can write your command as:
chflags hidden "$#"

do shell script with bracket within formula?

I want to create an applescript that inserts a separator into the dock. The code for that is:
defaults write com.apple.dock persistent-apps -array-add '{ "tile-type" = "spacer-tile"; }';killall Dock
I figured the easiest way to do it is by doing
do shell script "defaults write com.apple.dock persistent-apps -array-add '{ "tile-type" = "spacer-tile"; }';killall Dock"
but the Script Editor complains:
A identifier can’t go after this “"”.
How do I make a script with brackets within brackets? Better solutions for this problem are also very welcomed.
You have to escape each double quote with a backslash
do shell script "defaults write com.apple.dock persistent-apps -array-add '{ \"tile-type\" = \"spacer-tile\"; }';killall Dock"

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

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 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