I created a small script to control httpd/php/mysql and used XCode to create and link an interface to this script. Everything works ok. The problem is I'm trying to make the application window close after the red circle button is clicked, and is not happening. The app keeps running and only the window is closed.
I have tried using:
applicationShouldTerminateAfterLastWindowClosed_(sender)
The whole script is this:
script AppDelegate
#### PROPERTY LIST ####
property parent : class "NSObject"
property startApache : missing value
property restartApache : missing value
property stopApache : missing value
property editConfig : missing value
property editVHosts : missing value
property openDir : missing value
property resetConfig : missing value
property editPHP : missing value
property resetPHPConfig : missing value
property startMYSQL : missing value
property stopMYSQL : missing value
property restartMYSQL : missing value
property openDirMYSQL : missing value
property resetMYSQL : missing value
#### APACHE CMDs ####
on startApache_(sender)
do shell script "/usr/sbin/apachectl start" with administrator privileges
end startApache_
on restartApache_(sender)
do shell script "/usr/sbin/apachectl restart" with administrator privileges
end restartApache_
on stopApache_(sender)
do shell script "/usr/sbin/apachectl stop" with administrator privileges
end stopApache_
on editConfig_(sender)
do shell script "open -a /Applications/BBEdit.app /private/etc/apache2/httpd.conf"
end editConfig_
on editVHosts_(sender)
do shell script "open -a /Applications/BBEdit.app /private/etc/apache2/extra/httpd-vhosts.conf"
end editVHosts_
on openDir_(sender)
do shell script "open /Library/WebServer/Documents/"
end openDir_
on resetConfig_(sender)
display dialog "Are you sure you want to reset the httpd.conf to it's default settings?\n\n This cannot be undone!" with icon stop with title "Reset Configuration File"
do shell script "/usr/sbin/apachectl stop" with administrator privileges
do shell script "cp /private/etc/apache2/httpd.conf.pre-update /private/etc/apache2/httpd.conf ; cp /private/etc/apache2/extra/httpd-vhosts.conf.default /private/etc/apache2/extra/httpd-vhosts.conf" with administrator privileges
end resetConfig_
#### PHP CMDs ####
on editPHP_(sender)
do shell script "open -a /Applications/BBEdit.app /etc/php.ini"
end editPHP_
on resetPHPConfig_(sender)
display dialog "Are you sure you want to reset the php.ini to it's default settings?\n\n This cannot be undone!" with icon stop with title "Reset Configuration File"
do shell script "cp /etc/php.ini.default /etc/php.ini" with administrator privileges
end resetPHPConfig_
#### MYSQL CMDs ####
on startMYSQL_(sender)
do shell script "/usr/local/bin/mysql.server start" with administrator privileges
end startMYSQL_
on restartMYSQL_(sender)
do shell script "/usr/local/bin/mysql.server restart" with administrator privileges
end restartMYSQL_
on stopMYSQL_(sender)
do shell script "/usr/local/bin/mysql.server stop" with administrator privileges
end stopMYSQL_
on openDirMYSQL_(sender)
do shell script "open /usr/local/var/mysql/"
end openDirMYSQL_
on resetMYSQL_(sender)
display dialog "Are you sure you want to reset ALL MySQL databases to their default?\n\n This cannot be undone!" with icon stop with title "Reset All Databases"
display dialog "Type the new root password" default answer "" with hidden answer
set root_pass to text returned of result
if root_pass = "" then
display dialog "Root password cannot be blank!" buttons {"Cancel"} with icon Caution
error number -128
else
display dialog "MySQL DB will be recriated in the background.\n\nClick on reset and wait." buttons {"Cancel","Reset"}
do shell script "/usr/local/bin/mysql.server stop" with administrator privileges
do shell script "rm -rf /usr/local/var/mysql" with administrator privileges
do shell script "/usr/local/bin/mysqld --initialize-insecure"
do shell script "/usr/local/bin/mysql.server start"
do shell script "/usr/local/bin/mysqladmin -u root password '"&root_pass&"'"
display dialog "All done!\n\nDatabase reseted." buttons {"Ok"} with title "MySQL Reset Utility"
end if
end resetMYSQL_
on applicationWillFinishLaunching_(aNotification)
end applicationWillFinishLaunching_
on applicationShouldTerminateAfterLastWindowClosed_(sender)
return true
end applicationShouldTerminateAfterLastWindowClosed_
on applicationShouldTerminate_(sender)
return current application's NSTerminateNow
end applicationShouldTerminate_
end script
As #vadian said: Have a look if the File's Owner's delegate outlet is connected to App Delegate (your script) correctly:
BTW this is the default after creating a new CocoaApplescript-App. I just copied the handler
on applicationShouldTerminateAfterLastWindowClosed_(sender)
return true
end applicationShouldTerminateAfterLastWindowClosed_
into the script and everything worked fine!
Have fun, Michael / Hamburg
Related
Is there a way to write a "to do script" with administrator privileges like you can with a "do shell script" ? I have a script that I am writing that opens a terminal window and gets the size of another user account but I get permission denied errors. I can easily solve this by entering sudo before the command but I don't want to enter a password in the terminal window. I want to be prompted with a dialog so I can enter a password just like I would get if I used a "do shell script"
This is part of my script that I have:
tell application "Terminal"
activate
do script "sudo du -sh /Users/example"
end tell
I know that you can solve this also like this:
do shell script "du -sh /Users/example" with administrator privileges
but doing it like this opens a terminal window but does not start the command.
You can do it without involving Terminal.app at all
set userSize to do shell script "du -sh /Users/example | awk '{print $1}'" with administrator privileges
The awk command strips the size part from the result.
Your second option is correct, but it should not be used with a tell "Terminal" block at all.
With the script bellow (only the 2 lines), Terminal will not be open and the only window will be the one asking your admin password.
set UserSize to do shell script "du -sh /Users/test" with administrator privileges
display dialog UserSize
Before OS X 10.10 (Yosemite), I could ensure an applescript dialog had focus by telling the "current application" to activate:
tell current application
activate
set output to (do shell script "printf '" & hostsLine & commentString & "' >> /private/etc/hosts" with administrator privileges)
end tell
This would bring the password dialog to the front to type in. The dialog no longer appears with focus in Yosemite and you have to click on it before you can type.
Searching on stack exchange and elsewhere hasn't provided any solutions/workarounds.
Does anyone have a way that works in OS X 10.10?
UPDATE 1: In case anyone else runs into this problem, it appears to be a new sandboxing issue. A non-password dialog box gets focus properly. In fact, a non-password dialog before the "with administrator privileges" password dialog will cause the password dialog to also have focus. I added a dialog to confirm Safari was still the front app while testing and discovered this. As a workaround, I have added a preceding dialog with timeout of 1 (second) until a better solution can be found.
tell me -- alternate method: tell current application
activate
display dialog "Trying to get focus…" with title "Can't focus in Yosemite" buttons {"Cancel", "Idle"} cancel button "Cancel" giving up after (1)
set output to (do shell script "printf '" & hostsLine & commentString & "' >> /etc/hosts" with administrator privileges)
end tell
Interestingly, BBedit's Authenticated Save helper script (for the MAS version of the app) uses a "with administrator privileges" password dialog. But it gets focus properly in Yosemite. Something different between a native app call to applescript vs. an Applescript tell command for an app to do so?
on documentShouldFinalizeAuthenticatedSave(theDocument, tempFilePath, destinationPath)
-- on input: tempFilePath points to the contents of the document written to a temp file, ready to move to the destination; destinationPath is where the file should be copied.
-- on exit: if the operation succeeded, delete the temp file (or else the application will assume the operation failed) and return YES for success
-- this is pretty straightforward: "cp tmpFilePath destinationPath"
do shell script "cp" & " " & quoted form of tempFilePath & " " & quoted form of destinationPath with administrator privileges
-- now remove the temp file, this indicates to the application that we did the work
do shell script "rm" & " " & quoted form of tempFilePath
return true
end documentShouldFinalizeAuthenticatedSave
UPDATE 2: If anyone is curious, this part of a Safari helper script for my wife to block spammy popup ad sites like mackeeper.zeobit.com that have modal dialog boxes. Editing /etc/hosts was a little too complicated/daunting and she wanted a turnkey solution. I can post the whole script if there's interest. It works fine under 10.9, but the focus for password issue is annoying in 10.10.
You could try... Tell me instead of Tell current application. "Tell me" basically tells the applescript to activate and run the "do shell script" command. This makes more sense. "do shell script" is an applescript command so it makes sense to ask applescript to run it. Maybe this will help with your problem.
Good luck.
Personally, I would remove the need to enter a password. The simplest way to do this is to append the following line to the /private/etc/sudoers file.
wife_user_name ALL=(ALL) NOPASSWD:ALL
To remove the need for a password for all Administrator accounts, change the following line in the /private/etc/sudoers file from
%admin ALL=(ALL) ALL
to
%admin ALL=(ALL) NOPASSWD: ALL
Next, change your applescript line from
set output to (do shell script "printf '" & hostsLine & commentString & "' >> /private/etc/hosts" with administrator privileges)
to
set output to (do shell script "sudo printf '" & hostsLine & commentString & "' >> /private/etc/hosts")
Changes to the /private/etc/sudoers file can be accomplished by using the Terminal and TextEdit applications. Open the Terminal application and type the following commands:
cd ~/desktop
sudo cp -n /etc/sudoers /etc/sudoers.orignal
sudo cp /etc/sudoers sudoers.txt
sudo chmod ug+w sudoers.txt
open sudoers.txt
visudo -c -f sudoers.txt
sudo cp -X sudoers.txt /etc/sudoers
When done, the sudoers.txt file on your desktop can be put in the trash.
To undo your changes, use the command:
sudo cp /etc/sudoers.original /etc/sudoers
This was tested using OS X 10.10.1
Below is a brief explanation of what each command does:
cd ~/desktop
This makes sure you are working from your desktop folder.
sudo cp -n /etc/sudoers /etc/sudoers.original
This backups your sudoers file. The backup can be used to undo your changes. The -n option insures that an existing sudoers.original file will not be overwritten.
sudo cp /etc/sudoers sudoers.txt
Copies the sudoers file to your desktop. The .txt extension is added so OS X will know this is a text file.
sudo chmod ug+w sudoers.txt
Changes the file’s permissions to allow write access.
open sudoers.txt
Opens the file in the TextEdit application. You need to edit the file and save the changes.
visudo -c -f sudoers.txt
Checks the edited file for syntax errors. The output should be sudoers.txt: parsed OK.
sudo cp -X sudoers.txt /etc/sudoers
Copies the file back to the /etc directory.
I stumbled upon this post trying to get the password dialog to get focus in an Alfred script that I am working on. I finally got it to work reliably by adding tell me to activate prior to my shell command. The resultant script was:
tell me to activate
do shell script "<command>" with administrator privileges
Hope this works for you.
2015-01-08 Edit
Mike, I understand your question as: "I have an AppleScript with a do shell script that asks for an admin password, but I have to click on the password dialog before I can type in it. How can I ensure the dialog has focus, saving me the nuisance of clicking it?"
My response to you is: "What if your AppleScript never asked for a password in the first place? Wouldn't you be happier?"
If so, then here's a solution. That question demonstrates how you can bypass the dialog by supplying a password parameter along with the with administrator privileges parameter.
Ok, Mike this is my second try at an answer.
Instead of using
tell current application
activate
set output to (do shell script "printf '" & hostsLine & commentString & "' >> /private/etc/hosts" with administrator privileges)
end tell
as you proposed at the start of your above question, try using
tell current application
activate
tell me to set output to (doShellScript for "printf '" & hostsLine & commentString & "' >> /private/etc/hosts" with administratorPrivileges)
end tell
where the doShellScript handler would be
on doShellScript for command as text given administratorPrivileges:adminPriv as boolean : false
if adminPriv then
set message to (name of current application) & " wants to make changes. Type your password to allow this."
set again to ""
set pw to ""
set icn to 1
repeat
set fullCommand to "printf '%s\\n' " & (quoted form of pw) & " | sudo -p '' -S " & command
try
return do shell script fullCommand
on error eStr number eNum partial result rList from badObj to expectedType
set errorMessage to "Sorry, try again." & return & return & "sudo: 1 incorrect password attempt"
if eStr ≠ errorMessage or eNum ≠ 1 then
error eStr number eNum partial result rList from badObj to expectedType
end if
end try
set actual to again & message
set values to display dialog actual default answer "" with icon icn with hidden answer
set again to "Sorry, incorrect passord. Try again." & linefeed
set pw to text returned of values
set icn to 2
end repeat
end if
return do shell script command
end doShellScript
An example dialog is shown below.
Your application name and icon will be different. The main thing is that the focus will be on the password text box when the dialog pops up.
dave.
I had a similar issue for which I had an applescript execute a continuous ping in Terminal, and then a dialog box would pop up in order for user to click OK to kill that ping and start a different one. The issue was similar as the dialog box kept showing behind the Terminal window. As I would have to execute this script numerous times in an urgent situation with very limited time, this was very annoying. I was finally able to resolve it by telling System Events to wait 1 second after it starts the continuous ping, and then keystroke CMD+Tab to switch back to the running script before opening the dialog box. The CMD+Tab initially only intermittently resolved it until I added the delay 1.
Example of issue:
tell application "Terminal" to do script pingCommand
set userDialog to display dialog "Client Connection Test Completed" buttons {"Ping Server", "Cancel"}
With above code, the dialog would always come behind the Terminal window.
Example of resolution:
tell application "Terminal" to do script pingCommand
delay 1
tell application "System Events" to key code 48 using {command down}
set userDialog to display dialog "Client Connection Test Completed" buttons {"Ping Server", "Cancel"}
I was facing the same problem, and created a solution that would bring my applet's dialog to the front.
I wrote a function called ActivateMe() that replaces the "activate" or "tell me to activate" commands that would proceed the "display dialog" command.
This function gets the current process name, and then executes the following AppleScript commands via a shell script.
delay 0.1
tell application "System Events"
set frontmost of process \"" & processName & "\" to true
end tell"
Here's the function. Enjoy.
on ActivateMe()
set myPath to path to me as text
if myPath ends with ":" then
set n to -2
else
set n to -1
end if
set AppleScript's text item delimiters to ":"
set processName to text item n of myPath
if (processName contains ".") then
set AppleScript's text item delimiters to "."
set processName to text 1 thru text item -2 of processName
end if
set AppleScript's text item delimiters to ""
set a to "delay 0.1"
set b to "tell application \"System Events\""
set c to "set frontmost of process \"" & processName & "\" to true"
set d to "end tell"
do shell script "osascript -e '" & a & "' -e '" & b & "' -e '" & c & "' -e '" & d & "'"
end ActivateMe
Appears to be a bug in 10.10.x most likely due to sandboxing. Fixed in 10.11 (El Cap) as the unmodified script gets focus from the activate command once again like it did in 10.9 and earlier versions.
I'm trying to write an automator service to fire up virtualhost.sh in a terminal.
Using the Services context menu the dialog opens to ask for the name of virtual host, then runs an applescript to launch terminal and pass in the input text.
What I want is to pass in my username and password to admin privileges so that I don't need to pass it in the terminal with sudo.
This can be done with do shell script but that executes a bin/sh and the virtualhost.sh is a bash script so I get the error bin/sh: virtualhost.sh command not found
Alternately I can use do script with command but this doesn't allow me to pass in the user name and password.
My code looks like so:
on run {input, parameters}
set vhost to "virtualhost.sh " & input
tell application "Terminal"
activate
do shell script vhost user name "user" password "pass" with
administrator privileges
end tell
end run
This produces the bin/sh error previously mentioned.
With do script with command
on run {input, parameters}
set vhost to "virtualhost.sh " & input
tell application "Terminal"
activate
do script with command vhost user name "user" password "pass" with
administrator privileges
end tell
end run
This produces an escaping error: Expected end of line, etc. but found property.
Is there a way to do this correctly?
Not specifically familiar with AppleScript Studio, but you can do it in plain old AppleScript (which appears to have the same issue) if you provide a full path to virtualhost.sh. (Also, Terminal is not required with "do shell script".) Example:
set vhost to "/usr/local/bin/virtualhost.sh " & input
do shell script vhost user name "user" password "pass" ¬
with administrator privileges
You can also extend $PATH (which is by default /usr/bin:/bin:/usr/sbin:/sbin with "do shell script") to include the path to virtualhost.sh, e.g.:
set vhost to "{ PATH=$PATH:/usr/local/bin; virtualhost.sh " & input & "; }"
do shell script vhost user name "user" password "pass" ¬
with administrator privileges
If you want a relative path, you can put virtualhost.sh inside the script application or bundle (e.g. in Contents/Resources), either in Terminal or by control-clicking and choosing "Show Package Contents". Then use "path to me":
set vhostPath to "'" & POSIX path of (path to me) & ¬
"/Contents/Resources/virtualhost.sh" & "'"
set vhost to vhostPath & space & input
do shell script vhost user name "user" password "pass" ¬
with administrator privileges
Per the comment on my other answer, I'm posting a secondary answer more in the spirit of that there's a will, there's a way, but it's a different, more dangerous approach. However, it's the only solution I can think of to this particular requirement (to get the interactivity of Terminal, but without having to prompt for an administrator password while running a script as administrator).
This solution runs Terminal as root, which is what do shell script "command" with administrator privileges does. This is dangerous because you have an open Terminal window with root access, so carefully weigh benefits against potential consequences of, say, opening a new Terminal window and being at the Bash prompt as root.
For this reason, the Terminal instance that is opened is killed upon completion of the script; if the "kill" command is removed, be aware you'll get multiple instances of Terminal, rather than multiple windows within the same instance.
No idea if this works in AppleScript Studio (it works in AppleScript Editor), but I can't think of any reason why it wouldn't.
set input to "some_input"
set vhost to "/usr/local/bin/virtualhost.sh " & input
set kill to ¬
"terminal_pid=$(</tmp/terminal_pid); rm /tmp/terminal_pid; kill $terminal_pid"
-- launch Terminal as root, and save its process ID in /tmp/terminal_pid
tell application "Finder" to set beforeProcesses to processes
do shell script ¬
"/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal " & ¬
"&> /dev/null & echo $! > /tmp/terminal_pid" user name "user" password ¬
"pass" with administrator privileges
-- wait until the new Terminal is confirmed to be running
tell application "Finder"
repeat while (processes is equal to beforeProcesses)
do shell script "sleep 0.5"
end repeat
end tell
-- Perform script in root Terminal window that we just opened,
-- and kill Terminal when done to prevent open root prompt
-- and multiple processes.
tell application "Terminal"
activate
do script vhost & "; " & kill
end tell
-- optional: wait until Terminal is gone before continuing
do shell script "while [[ ( -f /tmp/terminal_pid ) " & ¬
"&& ( \"$(ps -p $(</tmp/terminal_pid) -o%cpu='')\" ) ]]; do sleep 0.5; done"
I've searched around but couldn't quite find anything to fit my problem.
I want to create a script to replicate the following:
Open Terminal
Execute the following command:
sudo kextunload /System/Library/Extensions/AppleHDA.kext
Then have it enter my OSX admin password for me.
Then execute the following:
sudo kextload /System/Library/Extensions/AppleHDA.kext
I'm completely new to applescript, so hoping someone can help me out.
Thanks!
The hint in a comment on the question is correct (in [Apple]Script Editor, select File > Open Dictionary..., select StandardAdditions.osax, then search for do shell script to see the complete syntax), but it's important to note that do shell script will NOT open a Terminal window; instead, it'll run the shell command hidden and return its result - which is generally preferable:
do shell script's return value is the shell command's stdout output.
If the shell command returns a non-zero exit code, AppleScript will throw an error and the error message will contain the command's stderr output.
To run commands with administrative privileges, you have 2 options:
[Recommended] Let AppleScript display a password prompt:
set shCmds to "kextunload /System/Library/Extensions/AppleHDA.kext;
kextload /System/Library/Extensions/AppleHDA.kext"
# This will prompt for an admin password, then execute the commands
# as if they had been run with `sudo`.
do shell script shCmds with administrator privileges
[Not recommended for security reasons] Pass the password as an argument:
set shCmds to "kextunload /System/Library/Extensions/AppleHDA.kext;
kextload /System/Library/Extensions/AppleHDA.kext"
# Replace `{myPassword}` with your actual password.
# The commands will run as if they had been executed with `sudo`.
do shell script shCmds ¬
user name short user name of (system info) password "{myPassword}" ¬
with administrator privileges
As stated, if something goes wrong - whether it is because of an invalid password or a canceled password dialog or the shell commands returning a non-zero exit code - a runtime error is thrown.
Here's an example of trapping it and reporting it via display alert.
try
do shell script shCmds with administrator privileges
on error errMsg number errNo
display alert "Executing '" & shCmds & "' failed with error code " & ¬
errNo & " and the following message: " & errMsg
return
end try
I do have several shell scripts whom are get activate not really behind each other.
But I want to stay in the same terminal window.
To simplify the problem, lets say that as example
first script. - cd to the working directory
second. - do some things and get files from that directory
third. - dome more thing on the newly files/folders in that directory.
fourth. - ....
How can I let script 2 and 3 ( all the script thus ) run in that very same directory and window that is opened during script 1?
F.e how can I avoid using the full path to that folder for every file.
EDIT: After adayzdone's answer I realize that I forgot to mention that I need also administrator privileges for one of the scripts
EDIT2: For now I use this
tell application "Terminal"
set newTab to do script
set current settings of newTab to settings set "Grass"
do script "bin/sh/ echo 'xx' | su;" in newTab
do script "cd " & quoted form of realParentPath in newTab
do script shellscript1 in newTab
do script shellscript..n in newTab
activate
end tell
but still no dice with the privileges.
try:
tell application "Terminal"
do script "cd ~/" in window 1
do script "ls -a" in window 1
end tell
OR
property usr : "username"
property pswd : "password"
set xxx to do shell script "cd ~/ ; ls -a " user name usr password pswd with administrator privileges