Skip to content

Instantly share code, notes, and snippets.

@mmichaa
Created March 16, 2013 21:56
Show Gist options
  • Save mmichaa/5178490 to your computer and use it in GitHub Desktop.
Save mmichaa/5178490 to your computer and use it in GitHub Desktop.
DevEnvToggle -- AppleScript for starting up and shutting down my Development Environment (Apache, MySQL, PostgreSQL) on Mac OS X.
script Service
property service_name : null
property service_state : null
on start_pre()
-- start pre-action here
end start_pre
on start_up()
tell me to start_pre()
-- start up action here
tell me to start_post()
end start_up
on start_post()
-- start post-action here
end start_post
on pre_down()
-- down pre-action here
end pre_down
on shut_down()
tell me to pre_down()
-- shut down action here
tell me to post_down()
end shut_down
on post_down()
-- down post-action here
end post_down
end script
script LaunchService
property parent : Service
property service_path_prefix : ""
property service_path_suffix : ".plist"
on start_up()
if my service_state = "shut_down" then
tell me to start_pre()
do shell script "sudo launchctl load -w " & my service_path_prefix & my service_name & my service_path_suffix with administrator privileges
tell me to start_post()
return true
else
return true
end if
end start_up
on shut_down()
if my service_state = "start_up" then
tell me to pre_down()
do shell script "sudo launchctl unload -w " & my service_path_prefix & my service_name & my service_path_suffix with administrator privileges
tell me to post_down()
return true
else
return true
end if
end shut_down
on resolve_state()
set result to null
try
set result to do shell script "sudo launchctl list " & my service_name with administrator privileges
on error errorMessage number errorNumber
set result to errorMessage
end try
if result contains "PID" then
set my service_state to "start_up"
else
set my service_state to "shut_down"
end if
return my service_state
end resolve_state
end script
script StartupService
property parent : Service
property service_up_indicator : null
on start_up()
if my service_state = "shut_down" then
tell me to start_pre()
set result to do shell script "sudo " & my service_name & " start" with administrator privileges
tell me to start_post()
return true
else
return true
end if
end start_up
on shut_down()
if my service_state = "start_up" then
tell me to pre_down()
set result to do shell script "sudo " & my service_name & " stop" with administrator privileges
tell me to post_down()
return true
else
return true
end if
end shut_down
on resolve_state()
set result to null
try
set result to do shell script "sudo " & my service_name & " status 2> /dev/null" with administrator privileges
on error errorMessage number errorNumber
set result to errorMessage
end try
if result contains my service_up_indicator then
set my service_state to "start_up"
else
set my service_state to "shut_down"
end if
return my service_state
end resolve_state
end script
script Apache
property parent : LaunchService
property service_name : "org.apache.httpd"
property service_path_prefix : "/System/Library/LaunchDaemons/"
end script
script MySQL
property parent : StartupService
property service_name : "/usr/local/mysql/support-files/mysql.server"
property service_up_indicator : "SUCCESS"
on start_pre()
set result to do shell script "mount"
if result contains "MySQL-Encrypted-Data" then
return true
else
set whoami to do shell script "whoami"
set image to "/Users/" & whoami & "/Library/Application Support/MySQL/EncDataImage.sparsebundle"
set label to do shell script "basename " & quoted form of image
set pass to do shell script "security find-generic-password -l " & quoted form of label & " -g /Users/" & whoami & "/Library/Keychains/login.keychain 2>&1 >/dev/null"
set pass to text ((offset of "\"" in pass) + 1) thru -2 of pass
do shell script "echo \"" & pass & "\\0\" | hdiutil attach -encryption AES-128 -owners on -stdinpass " & quoted form of image
return true
end if
end start_pre
on down_post()
set result to do shell script "mount"
if result contains "MySQL-Encrypted-Data" then
do shell script "hdiutil detach /Volumes/MySQL-Encrypted-Data"
return true
else
return true
end if
end down_post
end script
script PostgreSQL
property parent : LaunchService
property service_name : "com.edb.launchd.postgresql-9.2"
property service_path_prefix : "/Library/LaunchDaemons/"
end script
on run
repeat
set dialog_message to ""
set services to {Apache, MySQL, PostgreSQL}
repeat with srv in services
tell srv to resolve_state()
set dialog_message to dialog_message & (name of srv) & ": " & (service_state of srv) & return
end repeat
display dialog "Development Environment Switch" & return & return & dialog_message buttons {"Start", "Stop", "Cancel"} default button 3
set DlogResult to result
if button returned of result = "Start" then
repeat with srv in services
tell srv to start_up()
end repeat
--display dialog "Development Environment start up"
else if button returned of result = "Stop" then
repeat with srv in services
tell srv to shut_down()
end repeat
--display dialog "Development Environment shut down"
else
exit repeat
end if
end repeat
end run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment