Launch an application from AppleScript bundle - macos

I've created an AppleScript bundle - main.app from
on run
set appAlias to POSIX path of (path to resource "MyApp.app")
set cmnd to appAlias & "Contents/MacOs/MyApp &"
display dialog "You're going to launch" & cmnd buttons {"Ok"}
do shell script cmnd with administrator privileges
end run
MyApp.app resides in main.app/Contents/Resources
When I launch main.app it quits right after displaying dialog and asking username and password without starting MyApp.app.
What am I doing wrong?

One problem could be if there are any spaces in the path, then essentially the path will be incorrect. Therefore you should always use "quoted form of" to ensure spaces and other path characters are accounted for properly. Try this...
on run
set appAlias to POSIX path of (path to resource "MyApp.app")
set cmnd to appAlias & "Contents/MacOs/MyApp"
display dialog "You're going to launch" & cmnd buttons {"Ok"}
do shell script (quoted form of cmnd & " &") with administrator privileges
end run
Also, I think you need to be certain of the name of the unix executable inside MyApp.app. Most applescript apps have "applet" inside instead of the name of the app. So double check that. You may need this instead...
set cmnd to appAlias & "Contents/MacOS/applet"

Try:
on run
set appAlias to POSIX path of (path to resource "MyApp.app")
display dialog "You're going to launch" & appAlias buttons {"Ok"}
tell application "System Events" to open appAlias
end run
EDIT
on run
set appAlias to POSIX path of (path to resource "MyApp.app")
display dialog "You're going to launch" & appAlias buttons {"Ok"}
do shell script "open " & quoted form of appAlias with administrator privileges
end run

There was a silly mistake in my script. I need to write "MacOS" instead of "MacOs"
Here is the script which allows my application to create the files in the restricted area:
on run
set appAlias to POSIX path of (path to resource "MyApp.app")
set cmnd to appAlias & "Contents/MacOS/MyApp"
display dialog "You're going to launch" & cmnd buttons {"Ok"}
do shell script cmnd with administrator privileges
end run

Related

Applescript - submitting a password that contains an ampersand

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

OSX icon to open terminal to specific location

How would I create an icon that would open the terminal into a specific directory? Could also be into the directory where the icon lives.
You can use "Automator" this way:
Create an automator application
Add a "Run Script Applescript" action
Modify the code to:
on run {input, parameters}
tell application "terminal"
activate
set UnixPath to POSIX path of ((path to me as text) & "::")
do script with command "cd " & UnixPath
end tell
end run
Save it
Run it! Move it where you want, Run it...
Finally found the easiest answer to this: use Finder services. See http://www.howtogeek.com/210147/how-to-open-terminal-in-the-current-os-x-finder-location.

Applescript to open terminal, run command, and show - Not working

I´m trying to create a keyshortcut to open terminal in current folder. Looking around, I found this code to create a service (the part of adding the shortcut to this service is solved), only added things are the "; clear" and some of the "activate" so it shows
on run {input, parameters}
tell application "Finder"
activate
set myWin to window 1
set theWin to (quoted form of POSIX path of (target of myWin as alias))
tell application "Terminal"
activate
tell window 1
activate
do script "cd " & theWin & ";clear"
end tell
end tell
end tell
return input
end run
It is not working as i would like.
troubles:
it opens two windows in terminal, have no idea why. It has nothing to
do with the added "activate"… it has always donde that
if I select an item on finder ( a folder ) it opens its parent directory and i would
like it to open the selected folder
this is my very first try with Applescript so if the error is obvious i just can't see it
Thanks in advance
The do script command already opens a window in Terminal. Try it this way:
tell application "Finder" to set theSel to selection
tell application "Terminal"
set theFol to POSIX path of ((item 1 of theSel) as text)
if (count of windows) is not 0 then
do script "cd " & quoted form of theFol & ";clear" in window 1
else
do script "cd " & quoted form of theFol & ";clear"
end if
activate
end tell
I like the reopen approach better...
tell application "Finder" to set currentFolder to target of front Finder window as text
set theWin to currentFolder's POSIX path
tell application "Terminal"
if not (exists window 1) then reopen
activate
do script "cd " & quoted form of theWin & ";clear" in window 1
end tell

Applescript an app to change background of notification centre in mountain lion

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

Applescript Shell Script Progress

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

Resources