| 1 | (* Applescript to stop/start Myth background apps |
|---|
| 2 | For use with MacPorts install of Myth |
|---|
| 3 | Author: Craig Treleaven, ctreleaven at cogeco.ca |
|---|
| 4 | Version: 0.25.2 |
|---|
| 5 | Modified: 2012Jul11 - new |
|---|
| 6 | 2012Sep21 - log rotation |
|---|
| 7 | |
|---|
| 8 | NB - if mbe is running, we only stop it if it was launched under launchd |
|---|
| 9 | *) |
|---|
| 10 | set mysqld to " not running on this machine. Is it on another machine on your network?" |
|---|
| 11 | set rotatorStatus to "" |
|---|
| 12 | set logrotButton to "" |
|---|
| 13 | set mythbackend to "" |
|---|
| 14 | set mythbackendButton to "Donno" |
|---|
| 15 | set newline to " |
|---|
| 16 | " |
|---|
| 17 | set indent to space & space & space & space |
|---|
| 18 | set myResult to "" |
|---|
| 19 | |
|---|
| 20 | repeat until (myResult contains "Close") |
|---|
| 21 | if (do shell script "sudo launchctl list" with administrator privileges) contains ".logrotate" then |
|---|
| 22 | set rotatorStatus to "runs daily" |
|---|
| 23 | set logrotButton to "Disable log rotation" |
|---|
| 24 | else |
|---|
| 25 | set rotatorStatus to "is not scheduled" |
|---|
| 26 | set logrotButton to "Schedule log rotation" |
|---|
| 27 | end if |
|---|
| 28 | |
|---|
| 29 | set processes to do shell script "ps -Ac" |
|---|
| 30 | if the processes contains "mysqld" then |
|---|
| 31 | set mysqld to " running." |
|---|
| 32 | end if |
|---|
| 33 | |
|---|
| 34 | if the processes contains "mythbackend" then |
|---|
| 35 | set mythbackend to " running." |
|---|
| 36 | set mythbackendButton to "Stop MythBackend" |
|---|
| 37 | try |
|---|
| 38 | do shell script "mythshutdown --check" |
|---|
| 39 | on error |
|---|
| 40 | set mythbackend to " running but busy with something. Are you sure you want to shut down now?" |
|---|
| 41 | end try |
|---|
| 42 | else |
|---|
| 43 | set mythbackend to " not running." |
|---|
| 44 | set mythbackendButton to "Start MythBackend" |
|---|
| 45 | end if |
|---|
| 46 | |
|---|
| 47 | set myResult to display dialog newline & "Simple tool to start and stop Myth's background processes" & Â |
|---|
| 48 | newline & newline & newline & "Currently... " & Â |
|---|
| 49 | newline & newline & indent & "Log rotation " & rotatorStatus & Â |
|---|
| 50 | newline & newline & indent & "MySQL is" & mysqld & Â |
|---|
| 51 | newline & newline & indent & "MythBackend is" & mythbackend & newline & newline  |
|---|
| 52 | with icon note with title  |
|---|
| 53 | "Stop/Start Myth-related programs" buttons {logrotButton, mythbackendButton, "Close"} Â |
|---|
| 54 | default button "Close" -- cancel button "Close" |
|---|
| 55 | set myResult to button returned of myResult |
|---|
| 56 | if myResult contains "Start MythBackend" then |
|---|
| 57 | do shell script "sudo launchctl load -w /Library/LaunchDaemons/org.mythtv.mythbackend.plist" with administrator privileges |
|---|
| 58 | else if myResult contains "Stop MythBackend" then |
|---|
| 59 | if ((do shell script "sudo launchctl list" with administrator privileges) contains "mythbackend") then |
|---|
| 60 | do shell script "sudo launchctl unload -w /Library/LaunchDaemons/org.mythtv.mythbackend.plist" with administrator privileges |
|---|
| 61 | -- there is a longish delay while myth closes down. |
|---|
| 62 | else |
|---|
| 63 | display alert " MythBackend appears not to have been started in the normal fashion. Unable to shut down." message "Was mythbackend started directly from a command line session?" as warning |
|---|
| 64 | end if |
|---|
| 65 | set myResult to "Close" |
|---|
| 66 | else if myResult contains "Schedule log rotation" then |
|---|
| 67 | --check for existence of |
|---|
| 68 | if (FileExists("@PREFIX@/etc/logrotate.conf") and  |
|---|
| 69 | FileExists("@PREFIX@/etc/logrotate.d/logrotate.mythtv")) then |
|---|
| 70 | do shell script "sudo launchctl load -w /Library/LaunchDaemons/org.macports.logrotate.plist" with administrator privileges |
|---|
| 71 | else |
|---|
| 72 | display dialog "logrotate is not configured. Please see http://www.mythtv.org/wiki/MacPorts for instructions." buttons {"Close"} |
|---|
| 73 | set myResult to "Close" |
|---|
| 74 | end if |
|---|
| 75 | else if myResult contains "Disable log rotation" then |
|---|
| 76 | do shell script "sudo launchctl unload -w /Library/LaunchDaemons/org.macports.logrotate.plist" with administrator privileges |
|---|
| 77 | end if |
|---|
| 78 | end repeat |
|---|
| 79 | |
|---|
| 80 | on FileExists(theFile) -- (String) as Boolean |
|---|
| 81 | tell application "System Events" to return (exists file theFile) |
|---|
| 82 | end FileExists |
|---|