Go Back   The macosxhints Forums > OS X Help Requests > AppleScript



Reply
 
Thread Tools Rate Thread Display Modes
Old 06-28-2012, 10:41 PM   #1
powermac867
Prospect
 
Join Date: Jun 2012
Posts: 3
HELP creating a simple Applescript to Automater sleep timer

I am pulling my hair out trying to create a simple script or Automater script for my daughter.

She likes to sleep with her computer running at night listening to webpages or iTunes.

I want something that will pop up a box when clicked on for her so she can enter times in minutes such as 15, 30, 45, 60, 75, 90 etc and then will close and run in the background (This option is not crucial ) until the last 2-5 minutes remain and it can popup a box that says your computer is going to sleep. and then puts the computer to sleep and possably force close applications that may not allow it.
powermac867 is offline   Reply With Quote
Old 06-29-2012, 08:10 AM   #2
macosnoob
MVP
 
Join Date: Jan 2007
Posts: 1,766
There's a very old (OS 9-era) script that still sort of works under OS X over at Doug's Scripts: http://dougscripts.com/itunes/script...tspiritfadeout.

It doesn't do everything that you ask, but you might want to give it a try--or take it is a template and modify it to suit.
macosnoob is offline   Reply With Quote
Old 06-29-2012, 08:38 AM   #3
benwiggy
League Commissioner
 
Join Date: Aug 2006
Posts: 5,070
Quote:
Originally Posted by powermac867
and possably force close applications that may not allow it.

That might be the trickiest part in an AppleScript.

Is the option in Energy Saver System Preferences, where you can specify a time for your computer to shutdown, not good enough?
benwiggy is offline   Reply With Quote
Old 06-29-2012, 09:01 AM   #4
powermac867
Prospect
 
Join Date: Jun 2012
Posts: 3
Sorta figured it out

The issue I was having with the applescript was that if Safari was in full screen mode playing a video and this applescript kicked in the popup window in safari asking if I would like to close multiple tabs. So I disabled the checking of closing multiple tabs and all works for now. I would still like to figure out a way for her to choose from multiple times even if those times are preset. Here is a screenshot of what I have created so far.

BTW the applescript from dougs site wouldn't even run because it is a PowerPC app and won't open with Lion.
Attached Thumbnails
Click image for larger version

Name:	Screen Shot 2012-06-29 at 9.52.55 AM.png
Views:	57
Size:	60.9 KB
ID:	4538  
powermac867 is offline   Reply With Quote
Old 06-29-2012, 09:23 AM   #5
macosnoob
MVP
 
Join Date: Jan 2007
Posts: 1,766
Quote:
Originally Posted by powermac867

BTW the applescript from dougs site wouldn't even run because it is a PowerPC app and won't open with Lion.

Still on Snow Leopard myself. I have no experience with Lion. I didn't know Lion would prevent you from opening the script in AppleScript Editor. (One more reason I'm not moving forward just yet.) Here's the code, if you want to tinker:

Code:
(*
Street Spirit Fade Out
version 1.2

Copyright © 2001 Kevin Oliver, kevinoliver@mac.com
Modifications ©2001 Chad Gard, gard@indy.net
Modifications from many to reset volume back to its initial state 

This is my first attempt an AppleScript, so lets hope it doesn't go off crashing anything. heh.

Hmm what kind of license does this have? I don't know. But, you cannot sell it or make a profit off it. 
Use it to learn some AppleScripting. 

How about you just use it, be happy, and if you have some good ideas, email me, and I'll try to stick em in. 
*)


property SLEEP_COMPUTER : "Sleep"
property STOP_ITUNES : "Stop Playing"
property SHUT_DOWN_COMPUTER : "Shut Down"

property lastSleepLength : 30
property lastSleepType : STOP_ITUNES

global endTime, isGoingToSleep, initialVolume
global numDecreases, counter, hasSetInitialVolume
global osVersion

on idle
	set curTime to current date
	if (curTime + 60 ≥ my endTime) then
		iTunesFade()
		
		if (not my isGoingToSleep and curTime > my endTime) then
			-- make sure we finish the fade out...
			set finishedFade to false
			tell application "iTunes"
				set finishedFade to (sound volume = 0)
			end tell
			
			if (finishedFade) then
				set my isGoingToSleep to true
				my iTunesSleep()
				quit me
			end if
		end if
	end if
	
	-- quit the script if iTunes isn't running any mo'
	if counter mod 10 = 0 then
		tell application "Finder"
			if not (process "iTunes" exists) then
				quit me
			end if
		end tell
	end if
	set counter to counter + 1
	
	return 1
end idle

on run
	set isGoingToSleep to false
	set numDecreases to 0
	set counter to 0
	set hasSetInitialVolume to false
	tell application "Finder"
		set osVersionString to ((get version) as international text)
		set osVersion to (my getMajorVersion(osVersionString))
	end tell
	log osVersion
	try
		set endTime to (current date) + getSleepLength()
	on error number -128
		-- user canceled
		quit me
	end try
end run

on iTunesFade()
	tell application "Finder"
		if process "iTunes" exists then
			tell application "iTunes"
				if not hasSetInitialVolume then
					set hasSetInitialVolume to true
					set initialVolume to sound volume
				end if
				
				set numDecreases to numDecreases + 1
				set newVolume to initialVolume - (initialVolume / 60 * numDecreases)
				if sound volume > newVolume then
					set sound volume to newVolume
				end if
			end tell
		end if
	end tell
end iTunesFade

-- puts iTunes to sleep
on iTunesSleep()
	tell application "Finder"
		if process "iTunes" exists then
			tell application "iTunes"
				if player state is playing then
					-- finish the fade to 0
					set currentVolume to sound volume
					repeat
						if (currentVolume > 0) then
							set currentVolume to currentVolume - 1
							set sound volume to currentVolume
						else
							exit repeat
						end if
					end repeat
					
					-- figure out whether to pause, or stop for cds
					set cdPlaying to false
					set currentTrackContainer to (get container of current track)
					set numSources to (get index of last source)
					-- go through all the sources, and see if its an audio CD and that it is the one playing the current track
					repeat with i from 1 to numSources
						if (audio CD = (get kind of source i)) and (currentTrackContainer = (get audio CD playlist 1 of source i)) then
							set cdPlaying to true
							exit repeat
						end if
					end repeat
					
					if cdPlaying then
						stop
					else
						pause
					end if
				end if
				
				set sound volume to initialVolume --return volume to its previous state
			end tell
		end if
		
		if lastSleepType = SLEEP_COMPUTER then
			sleep
		else if lastSleepType = SHUT_DOWN_COMPUTER then
			shut down
		end if
		
	end tell
end iTunesSleep



-- gets the amount of minutes till we go to sleep
on getSleepLength()
	repeat
		set dialogResult to display dialog "How many minutes till we go to Sleep?" default answer my lastSleepLength
		try
			if text returned of the dialogResult is not "" then
				set sleepLength to text returned of dialogResult as number
				exit repeat
			end if
			beep
		end try
	end repeat
	
	-- decides whether we want to just stop iTunes, or put the computer to sleep/shutdown.  Could be done in 
	-- other dialog, but this is more personalble.  Also, would work well with speech listener as a question/response thing
	-- or, as a pull down menu in the first dialog if using dialog director.  But this option uses no non-standard osaxen
	--	
	-- for some reason, it appears OS X, doesn't handle the shut down command well from Stay Open applescripts, so we leave that option out 
	if (osVersion ≥ 10) then
		set sleepChoices to {"Cancel", STOP_ITUNES, SLEEP_COMPUTER}
	else
		set sleepChoices to {STOP_ITUNES, SLEEP_COMPUTER, SHUT_DOWN_COMPUTER}
	end if
	set sleepDialog to display dialog ¬
		"What do you want me to do when we go to sleep?" buttons sleepChoices ¬
		default button lastSleepType
	
	set lastSleepType to button returned of sleepDialog
	
	-- we store this in a property variable so we can remember the last setting
	set my lastSleepLength to sleepLength
	
	return sleepLength * minutes
end getSleepLength

on getMajorVersion(versionString)
	set buf to ""
	set strLength to (get length of versionString)
	set strStart to 1
	
	-- first try to see if its an international version that has a '-' (ie: Z1-9.2)
	repeat with i from 1 to strLength
		if ("-" = (get character i of versionString)) then
			set strStart to i + 1
			exit repeat
		end if
	end repeat
	
	repeat with i from strStart to strLength
		if ("." = (get character i of versionString)) then
			exit repeat
		else
			set buf to buf & (get character i of versionString)
		end if
	end repeat
	
	try
		set versionNumber to (buf as number)
	on error
		-- if we couldn't convert the version number, we use 10 because the script uses less features that way
		set versionNumber to 10
	end try
	
	return versionNumber
end getMajorVersion
macosnoob is offline   Reply With Quote
Old 06-29-2012, 09:01 AM   #6
macosnoob
MVP
 
Join Date: Jan 2007
Posts: 1,766
If it's just a matter of putting the machine to sleep, why quit or force quit any application?
macosnoob is offline   Reply With Quote
Old 06-29-2012, 09:07 AM   #7
powermac867
Prospect
 
Join Date: Jun 2012
Posts: 3
Just wanted to make sure that no running application would stop the computer from sleeping. I know some applications can prevent sleep because they are running in the background so covering bases.
powermac867 is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 01:51 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.