Saturday, July 14, 2012

How to write a launchd protocol


My Launchd protocol.

Here are the steps I did.
Step 1. Write application to launch: This is my application called beatrice2.app. All it does is open a website.
-       Use script editor
tell application "Google Chrome"
                            activate
                            set theUrl to "http://genome.ucsc.edu/cgi-bin/hgCustom?hgsid=267914753"
                           
                            if (count every window) = 0 then
                                                make new window
                            end if
                           
                            set found to false
                            set theTabIndex to -1
                            repeat with theWindow in every window
                                                set theTabIndex to 0
                                                repeat with theTab in every tab of theWindow
                                                                set theTabIndex to theTabIndex + 1
                                                                if theTab's URL = theUrl then
                                                                              set found to true
                                                                              exit repeat
                                                                end if
                                                end repeat
                                               
                                                if found then
                                                                exit repeat
                                                end if
                            end repeat
                           
                            if found then
                                                tell theTab to reload
                                                set theWindow's active tab index to theTabIndex
                                                set index of theWindow to 1
                            else
                                                tell window 1 to make new tab with properties {URL:theUrl}
                            end if
                           
end tell

Step 2: Save your app in /usr/bin/:
- Use terminal
- Here I copied the application from the desktop and pasted it in /usr/bin
- Next I checked to see if the application is in usr/bin
speedcongenics:Desktop samantha$ sudo cp /Users/samantha/Desktop/Beatrice2.app /usr/bin

speedcongenics:Desktop samantha$ cd /usr/bin

speedcongenics:bin samantha$ ls -la Beatrice2.app
-rwxr-xr-x@ 1 root  wheel  13780 May 15 08:06 Beatrice2.app

Side note: If you would like to delete the application from usr/bin:
To delete
  - Use terminal
samantha-s-imac:Desktop samanthataffner$ sudo rm -rf /usr/bin/beatrice2.app

Step 3: Write a .plist: This is my plist.  I called it: com.apple.beatrice.plist
-       I wrote this in TextWrangler http://www.barebones.com/products/TextWrangler/download.html
-       This plist calls my application in /usr/bin/Beatrice.app
-       It activates at 11:35AM every day
-       It also uses two files Beatrice.err & Bearice.out:  These help with debugging

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.apple.beatrice</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/bin/Beatrice.app</string>
    </array>
    <key>StartCalendarInterval</key>
    <dict>
        <key>Hour</key>
        <integer>11</integer>
        <key>Minute</key>
        <integer>35</integer>
      </dict>
      <key>StandardErrorPath</key>
  <string>/tmp/Beatrice.err</string>
  <key>StandardOutPath</key>
  <string>/tmp/Beatrice.out</string>
</dict>
</plist>

Step 4: Choose where you would like to save your plist at. This depends on what users this cronjob will be used for.

save .plist in for every user:  /System/Library/LaunchDaemons
save .plist in only for one user: /System/Library/LaunchAgents

-       Saved mine in LaunchDaemons

Step 5: Add .err and .out files
-       Use terminal
-       This will make the two files
speedcongenics:bin samantha$ cd /tmp
speedcongenics:tmp samantha$ ls
Beatrice.err                        launch-FiA4Y0                        launch-ajbvGL
Beatrice.out                        launch-IioWT4                        launch-bF7prV
com.hp.launchport            launch-JF2LIV                        launch-kx77NW
launch-1UjaTy                        launch-N51jhz                        launch-wTLkMD
launch-2zebDO                        launch-SijDxz                        launchd-90670.DuGDuP
launch-EjxSF5                        launch-TtNSBM
speedcongenics:tmp samantha$ sudo pico Beatrice2.err

Add # Hello push control X then Y (this makes the file)


Step 6: Change permissions of your .plist file
- Use terminal
- Some of the explanations have been modified from:
            - See what your permissions are by doing the following
speedcongenics:tmp samantha$ cd /System/Library/LaunchDaemons
speedcongenics:LaunchDaemons samantha$ ls -la com.apple.beatrice.plist
-rw-r--r--@ 1 root  wheel  523 May 11 19:57 com.apple.beatrice.plist
- The very first letter in a file's ls -la listing states what type of file it is. For example:
-rw-r--r--@ 1 root  wheel  523 May 11 19:57 com.apple.beatrice.plist
.plist is a regular file as its listing begins with the character -.
- The next nine characters represent the file's permissions. Permissions are always listed in the order of read, write, and execute. If the letter is listed, the permission is granted; if there is a - instead of the letter, that permission is denied. The permissions are repeated three times to represent owner, primary group, and everyone else. In the following listing:
-rw-r--r--@ 1 root  wheel  523 May 11 19:57 com.apple.beatrice.plist
- As before, .plist is a regular file since its listing begins with a -. The owner of the file (root) has read and write permissions, and without execute permission. Anyone in the primary group (wheel) has read permission to this file, but not write or execute permission. Everyone else has read permission, but not write or execute permission.
- These permissions can be changed by the following commands in the terminal.
            - now we see my file has root (rw-), wheel (r--), and everyone else (r--)
-  Here I explain a little about the chmod from what I’ve learned from the links listed above
- Since everything in Unix is a file and every file has permissions for the owner (root), group(wheel), and world
            0 = no operations
            1 = execute permission or the ability to cd in the case of a directory
            2 = write permission
            4 = read permission
-       These numbers are added together.  Here are examples
o   644 = ((4(read) + 2(write))Root permission) + ((4(read))Wheel permission) + ((4(read))Everyone else permission)
o   755 = my file needs to be -rwxr-xr-x for this cronjob
§  The commands below changes my .plist to -rwxr-xr-x
Please NOTE: I’ve changed computers from here on.  So locations of my file also changed.
samantha-s-imac:LaunchDaemons samanthataffner$ sudo chmod 755 com.apple.beatrice.plist
Password:
samantha-s-imac:LaunchDaemons samanthataffner$ ls -l com.apple.beatrice.plist
-rwxr-xr-x@ 1 root  wheel  662 May 14 11:33 com.apple.beatrice.plist

Step 7: Now we have to launch our .plist.  This will have to be done after every modification of the .plist or the .app.
            - Use terminal              
samantha-s-imac:LaunchDaemons samanthataffner$ sudo chgrp wheel com.apple.beatrice.plist
samantha-s-imac:LaunchDaemons samanthataffner$ sudo launchctl unload /System/Library/LaunchDaemons/com.apple.beatrice.plist
samantha-s-imac:LaunchDaemons samanthataffner$ sudo launchctl load /System/Library/LaunchDaemons/com.apple.beatrice.plist
samantha-s-imac:LaunchDa