Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Installing Sleepwatcher for an AppleScript...

I have managed to install Sleepwatcher successfully.... I think. If I go into the terminal and do a man sleepwatcher, it'll pull up the dialog for sleepwatcher. So I assume I've got it installed correctly. Where I am stuck is pointing it to run my AppleScript named WakeDialogueForReciepts. I have looked and looked and keep getting stuck. Nothing I seem to do is working. So! Can someone give me very simple instructions to make this run? I have no bash experience really.

MacBook Pro, Mac OS X (10.7.2)

Posted on Nov 20, 2011 6:37 PM

Reply
12 replies

Nov 20, 2011 9:39 PM in response to chef855

It would help a lot if you described some of what you'd done to date - might help us point you in the right direction.


Ultimately, SleepWatcher depends on some UNIX familiarity. Once installed it will look for a file in your home directory called '.sleep'. This should be a shell script that contains the commands you want to run on sleep (or a corresponding .wakeup file for commands to do on wake - it's not clear where you want your script to go.


Beyond that, these files need to be shell scripts, not AppleScripts. You can invoke your AppleScript a number of ways, depending on how the script is saved.

If your script is saved as a simple .scpt file then the easiest way is to add an osascript command to your .sleep/.wakeup file which just needs the path to the file:


#! /bin/bash


osascript /path/to/WakeDialogueForReciepts.scpt


Note that this will require both the full path to the file, and the .scpt filename extension that's usually hidden in the Finder.


This might all be known, and tried, but since you don't say what you've done so far it's impossible to tell.

Jan 25, 2012 10:37 AM in response to chef855

I did mine this way. The following is my '.wakeup' file code for SLEEPWATCHER.

The '.wakeup' file is in my directory ~/


The first two lines of code set up the script to accept APPLESCRIPT right in the '.wakeup' script.

(Do not change the first two lines. Write the rest of your code in APPLESCRIPT Editor to ensure it is correct. Then paste it into the '.wakeup' file after the first two lines.)


#!/bin/sh

exec <"$0" || exit; read v; read v; exec /usr/bin/osascript - "$@"; exit

delay 10

set testnet to 1

repeat with idx from 1 to 5

try

set ping_result to (do shell script "ping -c 1 -t 1 192.168.20.70")

set testnet to 10

on error

if testnet = 1 then

say "Searching for W Net Drive"

end if

beep 3

set testnet to (testnet + 1)

delay 10

end try

if testnet = 10 then

exit repeat

end if

end repeat

if testnet = 10 then

delay 1

mount volume "smb://W-NetDrive/Volume_1"

say "Net Drive Volumes Mounted"

end if

if testnet = 6 then

say "The W Net Drive could not be PINGED on the network"

say " I am unable to mount the W Drive Volumes"

end if

Feb 24, 2012 1:13 PM in response to Kevsappleman

I have simplfied this.

This is waht is in my .wakeup file



#! /bin/bash

osascript ~/Documents/Data-K/W_NetDrive_PING/W-PING-STARTUP.scpt



This is now my script W-PING-STARTUP.scpt (i am getting better at this)



set testnet to 0

say "waking" using "Victoria"

delay 0.3

say "up" using "Victoria"

repeat with idx from 1 to 5


try

set ping_result to (do shell script "ping -c 1 -t 1 192.168.20.70")

set testnet to 10


on error

delay 10

end try


end repeat


if testnet = 10 then

mount volume "smb://W-NetDrive/Volume_1"

say "Net Drive Volumes Mounted" using "Victoria"


delay 20


beep 1

end if


if testnet = 0 then


beep 5

end if



Just having issues getting Sleepwatcher to call the script. I have tested the .wakeup file, as if I click on it to executes it it alls the script. So just have to figure out what happened to the Sleepwatcher side and why id no longer calls the .wakeup file.


I'll figure this out sooner or later.

Mar 26, 2012 8:13 PM in response to chef855

Hello there!


I have a few answers for you that should help you out and make this a very useful tool for you. However, I do not want to take any accountability if messing around with shell scripts hurts your computer. There are many risks in doing this. But these instructions should help you quite a bit (especially if you are a beginner at the Terminal, like myself 😉 )


1. It seems you have installed Sleepwatcher correctly. If, by chance, these instructions do not work, I suggest reinstalling Sleepwatcher via MacPorts. MacPorts is one of the most effective and simplest ways to install Terminal based add-ons. First, of course, install MacPorts, which is as easy as downloading a package and running it. To install Sleepwatcher via MacPorts, simply type in the command line:

sudo port install sleepwatcher

then type: sudo port load sleepwatcher

Sleepwatcher will then automatically be installed and loaded.


2. You need to actually convert your AppleScripts into a Shell Script (they are not the same). The way to do this is to load it into the .sleep or .wakeup script that you desire to be placed in your Home folder. The way to do this is to do the following:


To Create a Script to be Run When Your Computer Goes to Sleep/Wakes Up:

a.) type in a new Terminal Window:

cat > $HOME/.wakeup

then

#!/bin/sh


b.) The next thing to understand is that you can run Applescripts through Terminal, via something called osascript, which is a command given within the Terminal. If your Applescript is fairly simple (just ejecting a disk or opening a new Finder window), I would go ahead and write that in osascript language. To gain a better idea of what that means, go to this website. However, it looks like you've saved your Applescript as a file. This next solution might be a little bit simpler. The following will help you just run your Applescript from the file you've saved it as (assuming it is a .scpt or something).


After typing the above commands, type: osascript followed by a space. Then drag your Applescript file to the Terminal window. Thus, your command should look something like: osascript /path/to/desired/script


c.) Hit enter.

d.) Hit ^D (Ctrl-D)

e.) Then type chmod +x $HOME/.wakeup


To do this for a script that runs when put to sleep, replace everything above that says ".wakeup" with ".sleep"



NOTE: If your script requires some sort of User Input (i.e. a display dialog, etc.), it will not be able to be run from within the Terminal. So, a workaround for this is to create an Application in Automator. I'm assuming you know how to do this, but if not, just start a new Application and drag the workflow "Run Applescript". Then copy and paste your Applescript text from your "WakeDialogForReceipts" (or whatever script you want) and save it as an Application in your Applications folder. Then, when typing the osascript in Terminal for the .wakeup script, type:

osascript -e 'tell application "WHATEVER YOU NAMED YOUR AUTOMATOR APPLICATION" to activate'


Then finish the rest of the steps above.



I hope this helps! I'll check back here often to make sure that you got the answer you were looking for! I just recently figured this out, and it has changed my life!


I must give all the credit to the MintyFresh blog. Their descriptions helped me a ton! Go there for more insight on Sleepwatcher and other things!

Apr 13, 2012 1:34 PM in response to Kevsappleman

Actually even simplier is to have the first line like this:

#/usr/bin/osascript

    set testnet to 0

    say "waking" using "Victoria"

    delay 0.3

    say "up" using "Victoria"

    repeat with idx from 1 to 5

       ...


Note that sleepwatcher has some problems in Mountain Lion because the sleep completes before sleepwatcher can finish executing, so whatever you do in .sleep make it snappy.


Also -- just a heads up -- launching programs from within .wakeup sometimescauses the launched application to be considered "not in the user's login session (i.e., in a different context) meaning it is unable to obtain passwords from keychain. An example of that is launching Adium from a .wakeup. You're better off setting it to offline in .sleep and back to online in .wakeup rather than launching it.

Apr 16, 2012 2:12 PM in response to dgerrity

I have managed to get this to work and it is working well


This is my .wakeup file


#! /bin/bash


osascript ~/Documents/Data-K/W_NetDrive_PING/sleep_and_wake_files/WAKEUP.scpt



This allows me to call a script that I can more easily test. As long as I keep the script simple is all works well.

This is my wakeup.scpt



set testnet to 0

--say "waking" using "Victoria"--

--delay 0.3--

--say "up" using "Victoria"--

repeat with idx from 1 to 5


try

set ping_result to (do shell script "ping -c 1 -t 1 192.168.20.70")

set testnet to 10


on error

delay 10

end try


end repeat


if testnet = 10 then

mount volume "smb://W-NetDrive/Volume_1"


--say "Net Drive Volumes Mounted" using "Victoria"--


beep 1

end if


if testnet = 0 then


--say "The W Net Drive could not be PINGED on the network"--


beep 5

end if





This is my .sleep file



#! /bin/bash


osascript ~/Documents/Data-K/W_NetDrive_PING/sleep_and_wake_files/sleep.scpt



Then it calls this sleep.scpt



delay 1

beep 1

--say "sleep ee time" using "Victoria"--




I don't have anything in the Sleep side. As you can see I removed all the talking. It was handy to listen to as it was processing things. But got anoying after a while. It was great as I could hear what stages it was at and what it was missing, etc. Just left it in there incase I needed it again. But all is working well.


Thanks for the comments.

May 15, 2012 12:15 AM in response to dgerrity

Oops. Just realized the first line should be the so-called shebang: hash bang or pound exclamation. Hope my little omission didn't screw you up.


Again, applescript is just another scripting language like perl or python or ruby to unix. So putting the shebang in the front identifies the scripting engine to launch with the accompanying script. it is, indeed a bit harder to debug this way; nonetheless you can debug the script in applescript, then copy it into a text file, use chmod +x to make it executable, and add the #!/usr/bin/osascript as the first line -- the rest is applescript.

Sep 17, 2012 10:52 AM in response to dgerrity

I've managed the '.wakeup' script to work, but the '.sleep' script is performed only after the wakeup and not before.

Running 10.8.1 here, having sleepwatcher 2.2 installed.


In '.sleep' script I have the following:


#!/bin/sh

/usr/sbin/diskutil unmountDisk /dev/disk2


if I run it manually from Terminal it performs as expected, eject my TimeMachine harddrive connected via USB.


Is there anyone successfully running the '.sleep' script before sleep on 10.8.x?

Oct 17, 2012 4:59 PM in response to chef855

Hi all. I have what should be a simple problem for those of you that know more than I do. I have installed sleepwalker. Now all I want to do is run two commands when I have logged in either from a stop or from a sleep or from a screen sleep. Right now what I have to type in terminal is


sudo bmdp capstop

sudo bmdp capstart


to stop and start the capture program for bitmeterOS. After the first command I have to put in my password.


However I know nothing about scripts and I am lost trying to figure out how to do this. If you could help I would be really grateful!!

Installing Sleepwatcher for an AppleScript...

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.