I have been attempting to create an Applescript that will enable a High Sierra secure token on an account (via Jamf's Self Service). It worked fine until an attempt was made for someone whose password contained an ampersand character within the password. Is there a specific syntax needed when using 'run shell script' within an applescript in order to avoid problems with ampersands contained within a variable? Thanks for any help with this one.
This is the applescript:
#!/usr/bin/osascript
set the_folder to (path to users folder)
tell application "Finder"
set foldernames to name of every folder of the_folder
end tell
set theChosenOne to choose from list foldernames with prompt "Select user to receive secure token"
display dialog "Password for " & theChosenOne default answer "" with icon note buttons {"Cancel", "Continue"} default button "Continue" with hidden answer
set thePassword to text returned of the result
do shell script "sysadminctl interactive -secureTokenOn " & theChosenOne & " -password " & thePassword
set theVerification to do shell script "sysadminctl interactive -secureTokenStatus " & theChosenOne & " 2>&1 | awk -F']' '{print $2}'"
display dialog theVerification
return
In order to escape an ampersand in AppleScript, you need to wrap the string (or the variable) with quoted form.
In your case:
set thePassword to quoted form of text returned of the result
Related
I am making a script for processing receipts and would like to be able to select a file than trigger a keyboard maestro applescript action that opens the selected file in quicklook than on a different area of the screen run the script below so that the user can input the data while looking at the file
display dialog "Date?" default answer ""
set response_date to text returned of result
display dialog "Payee?" default answer ""
set response_payee to text returned of result
set account_choices to {"Bank1", "Bank2", "Cash"}
set response_account to choose from list account_choices with prompt "Select Account:" default items {"Bank1"}
display dialog "Amount?" default answer ""
set response_amount to text returned of result
set filename to response_date & "-" & response_payee & "-" & response_account & "-" & response_amount & ".pdf"
display dialog filename
The shell command qlmanage with option -p followed by your file path activates the Quick Look feature, same as the space bar on the Finder. Example:
Set myFile to "/HD/Users/me/Desktop/image.jpg"
do shell script "qlmanage -p " & myFile's quoted form
For more options, use Terminal with command: man qlmanage
I'm trying to write some text into an xml file situated in a subfolder of the main library/Application Support folder using a shell script
do shell script "echo '" & theText & "' > " & thePath.
Without a password, I get a "sh: /Library/Application: Permission denied" which is perfectly logical.
Adding user name and password, as shown in the code below, I no longer get any error, but no text is written to the file.
If I put a wrong user name or password, it gives me "The user name and password were incorrect", which shows that the password is indeed being taken into account.
Am I trying to do something impossible, or is my code missing something ?
set thePath to POSIX path of ("/Library/Application Support/MyApp/Stuff/test.xml" as text)
set theText to "<ProductSpecific><Visibility type=\"Number\">3</Visibility></ProductSpecific>"
set theScript to "echo '" & theText & "' > " & thePath
do shell script theScript user name "myshortusername" password "mypassword" with administrator privileges
I should get theText written to /Library/Application Support/MyApp/Stuff/test.xml
but nothing is written although I don't get an error message either ! Oh, and if I move the file to the desktop and change the path, it all works fine !!
The reason of the error is the space character in Application Support. You have to escape the entire path.
It's highly recommended to use always quoted form of
set theScript to "echo " & quoted form of theText & " > " & quoted form of thePath
Side note:
The string path is already a POSIX path (and it's already text) so you can omit both
set thePath to "/Library/Application Support/MyApp/Stuff/test.xml"
I'm currently trying to create an AppleScript Application which will copy a folder from its "Contents" folder to the user's desktop. The code I have at the moment is:
set theFile to path to me as string
set hfsme to theFile & "Contents:Folder"
set posixPath to POSIX path of hfsme
set contentfolder to posixPath
set AppleScript's text item delimiters to " "
set textItem to text items of contentfolder
set AppleScript's text item delimiters to "\\ "
set contentfolder to textItem as string
set AppleScript's text item delimiters to {""}
do shell script "cp -a " & contentfolder & " $HOME/Desktop"
However, I receive the following error upon run:
"cp: /Users/myusername/Desktop/Test Application.app/Contents/Folder: No such file or directory"
I think this is because I expect the copy location to be:
/Users/myusername/Desktop/Test\ Application.app/Contents/Folder
But the script fails to add the backslash before the space which is what the shell command requires.
My question is: how do I replace the spaces in the variable with "\ " ?
P.S. Before suggesting simply renaming the application a name which doesn't have a space, it is very important for me to have an application name with a space.
I have also tried using "duplicate" command with AppleScript but wasn't able to get this to work but I am open to suggestions!
(Please bear in mind that I'd like this script to work on other people's computers as well meaning that I cannot assume I already know the user's username.
Thank you in advance for any help!
Kind regards, Tom
Just add quoted form of, it handles the quotation in a very smart (and reliable) way.
set theFile to POSIX path of (path to me)
set contentfolder to theFile & "Contents/Folder"
do shell script "cp -a " & quoted form of contentfolder & " $HOME/Desktop"
If do shell script does not support $HOME write
do shell script "cp -a " & quoted form of contentfolder & space & quoted from of POSIX path of (path to desktop)
I wanted to create an app to change the background of the notification centre so applescripted it. Someone please tell me what's wrong with my code.
set NCBGPath to "/System/Library/CoreServices/Notification Center/Contents/Resources/"
set NCBackground to "linen.tiff"
set themeFolder to (choose folder with prompt "Choose a Theme") as text
set themePath to themeFolder & NCBackground
set posixNCPath to NCBGPath & NCBackground
set shouldCopy to false
tell application "Finder"
if exists file themePath then set shouldCopy to true
end tell
if shouldCopy then
do shell script "cp " & quoted form of POSIX path of themePath & space & quoted form of posixNCPath with administrator privileges
-- you probably should correct the file permissions too as the copied file probably won't have the proper owner and stuff
else
display dialog "Could not find the background file in the chosen folder."
end if
Possibly this: "If cp detects an attempt to copy a file to itself, the copy will fail."
What happens if you change the last if statement to:
if shouldCopy then
do shell script "cp " & quoted form of POSIX path of themePath & space & quoted form of NCBGPath with administrator privileges
-- you probably should correct the file permissions too as the copied file probably won't have the proper owner and stuff
else
display dialog "Could not find the background file in the chosen folder."
end if
I am creating an applescript that creates a backup image of the /Users folder in Mac Os X. I would like to do one of two things:
Either display a barber pole progress bar while the shell script is running with an option to quit.
Display a dialog box while the script is running with an option to quit.
I have tried doing the shell script with the /dev/null but that ignores all output. I would like to save the output and display it in a dialog for the user.
Here is my code:
set computername to (do shell script "networksetup -getcomputername")
set fin to ""
set theIcon to note
tell application "Finder"
try
set folderalias to "/Users"
set foldersize to physical size of folder (alias ":Users") --(info for folderalias)
set foldersize to (round ((foldersize / 1.0E+9) * 100)) / 100
on error
tell application "System Events"
set foldersize to (round (((get physical size of folder ":Users" as text) / 1.0E+9) * 100)) / 100
end tell
end try
end tell
display dialog "User profile backup utility" & return & return & ¬
"The computer name is: " & computername & return & return & ¬
"The '/Users/' directory size is: " & "~" & foldersize & " GB" & return & return & ¬
"Would you like to backup the '/User' directory now?" & return ¬
buttons {"Cancel", "Backup Now"} default button "Backup Now"
set comd to "hdiutil create ~/Desktop/" & computername & ".dmg -srcfolder /test/"
set fin to do shell script (comd) with administrator privileges
display dialog fin
Displaying a progress bar dialog is not possible with on-board AppleScript (i.e. Standard Additions), but this can be achieved using Shane Stanley’s ASObjC Runner, a scriptable faceless background application which provides, among many over useful functions, a set of progress dialog related commands. Once downloaded onto your system,
tell application "ASObjC Runner"
reset progress
set properties of progress window to {button title:"Abort Backup", button visible:true, message:"Backing up the '" & (POSIX path of folderalias) & "' directory.", detail:"There are " & foldersize & " GB of data to backup – this might take some time.", indeterminate:true}
activate
show progress
end tell
try -- to make sure we destroy the dialog even if the script error out
<your backup operation here>
end try
tell application "ASObjC Runner" to hide progress
will show an indeterminate progress bar (or “barber pole”) while the backup operation runs – at least if it is synchronous (as shell commands called from AS are). As to the output of your shell command, that is automatically returned by the do shell script command – in your code, it is assigned to fin [code lifted more or less wholesale from the ASObjC Runner documentation].
ASObjC Runner can be embedded into an AppleScript application (save your script as an application in AppleScript Editor) by putting it into the bundle’s Resources folder (in Finder, select Show Package Contents in the context menu) and using the path to resource command to call it inside a using terms from block – the documentation I linked to above has details and example code, but, crucially, contains one critical error: your tell statement needs to use the POSIX path to the Resources bundle (tell application (POSIX path of (path to resource "ASObjC Runner.app"))).
A few remarks on your code:
there is a more elegant way to get an alias to the /Users folder:
path to users folder
– no need for hardwiring and calls to Finder. You can then get the shell compatible path to that by using POSIX path of, or, if you need it quoted, quoted form of POSIX path of of it.
I’d recommend using only System Events to get the physical size – unlike Finder, it works in the background. That will allow you to get rid of the tell application "Finder" and try … catch blocks (not sure what you meant to achieve by that one anyway – if you were reacting to error -10004, that is because round does not like to be put inside a tell block).
No need to initialize fin to an empty string – you will get a return value from do shell script.
Speaking of your do shell script, you need to quote the computerName variable of it will break on spaces in that name.
theIcon is never used.
You might want to use display alert instead of display dialog for the user confirmation, as it has a nice emphasis on the first part of the message.
There are a lot of unnecessary parentheses in the code – AppleScript needs some of these to delimit semantic command blocks, but not all of them…
All together mean your code can be modified to:
set computerName to do shell script "networksetup -getcomputername"
set folderAlias to path to users folder
tell application "System Events" to set folderSize to physical size of folderAlias
set folderSize to round(folderSize / 1.0E+9 * 100) / 100
display alert "User profile backup utility" message ¬
"The computer name is: " & computerName & return & return & ¬
"The '" & (POSIX path of folderAlias) & "' directory size is: " & "~" & folderSize & " GB" & return & return & ¬
"Would you like to backup the '" & (POSIX path of folderAlias) & "' directory now?" & return ¬
buttons {"Cancel", "Backup Now"} default button "Backup Now"
set shellCmd to "hdiutil create ~/Desktop/'" & computerName & "'.dmg -srcfolder /test/"
-- progress bar dialog display initialization goes here
try
set shellOutput to do shell script shellCmd with administrator privileges
end try
-- progress bar dialog hiding goes here
display dialog shellOutput
Although not as pretty as kopischke's suggestion, a quick and dirty way to get progress information is to run the command in the terminal itself.
set comd to "hdiutil create -puppetstrings '~/Desktop/" & computername & ".dmg' -srcfolder '/test/'"
tell application "Terminal" to do script comd