What is AppleScript?

image: https://lifetrg.files.wordpress.com/2012/11/1_users_jbkim_dropbox_blog_applescriptalarm_applescript_editor_logo8.png

AppleScript is a computer language that Apple made. Using this language you can control various applications. Although limited, in some cases you can order your Mac to do repetitive tasks that are tedious.

AppleScript Editor

To order the computer you need a place to write down your orders. This place is AppleScript.

image: https://lifetrg.files.wordpress.com/2012/11/0_users_jbkim_dropbox_blog_applescriptalarm_applescripteditor8.png

It looks like this. You can run your commands by pressing “Run”.

Why I made the alarm

The reason was easy. I wanted to wake up to music. Of course I could have bought an application to do this but why do this when you have AppleScript. One more thing I wanted was to listen to music until I fell asleep.

How the alarm works (Algorithm)

How the alarm works is simple.

1. Enter the time you want to wake up.

2. Turn on the music. ( Set the volume to 30 )

3. Turn off the music after 30 minutes.

4. Turn on the music at the time you want to wake up.

All you need to do this is to copy the code below into the AppleScript Editor. Then it will look like this.

image: https://lifetrg.files.wordpress.com/2012/11/10_users_jbkim_dropbox_blog_applescriptalarm_applescripteditor_en.png

Alarm code

on run 

	# Gets the wake up time from the user.
	set targetTime to text returned of (display dialog "Enter the time you want to wake up:" default answer "" & (current date)) 
	## Corrects the date 
	if (current date) > (date targetTime) then 
		set subTargetTime to (date targetTime) + (24 * hours) 
		set subTargetTime to (date targetTime) 
	end if 
	display dialog "See you at " &(subTargetTime as string) & "." 
	# display dialog "Delay " & (subTargetTime - (current date) ) & " seconds." 

	# Turn on the music.
	tell application "iTunes" 
		set sound volume to 30	
	end tell

	# Turn off the music after 30 minutes.
	delay 60 * 30 
	tell application "iTunes" 	
	end tell

	# Turn on the music at the alarm time.
	delay subTargetTime - (current date) 
	tell application "iTunes" 
		set sound volume to 70	
	end tell

	# display dialog "Finished" 
end run

How it looks when running the alarm

After copying the code to the AppleScript editor, if you press Run (in the AppleScript Editor) then you will get this window.

image: https://lifetrg.files.wordpress.com/2012/11/7_users_jbkim_dropbox_blog_applescriptalarm_applescriptdialog_en.png

Enter the time you want to wake up.

image: https://lifetrg.files.wordpress.com/2012/11/8_users_jbkim_dropbox_blog_applescriptalarm_applescriptdialog-2_en.png

It will show the time of the set alarm.

image: https://lifetrg.files.wordpress.com/2012/11/9_users_jbkim_dropbox_blog_applescriptalarm_applescriptdialog-3_en.png

Then music will start playing and after 30 minutes it will turn off. Then it will play the music once again at the set alarm time.

If you want to stop the apple script, just press Stop ( in the AppleScript Editor )

How to make an alarm program

If you came until here, you have learned how to make an alarm using apple script.

But it is tedious to always open the apple script and pressing Run. If you make the script into an application you will just need to run the application.

It is easy to make an application from AppleScript. In the AppleScript Editor goto File->Save as… And then in the File Format, change it to Application.

image: https://lifetrg.files.wordpress.com/2012/11/5_users_jbkim_dropbox_blog_applescriptalarm_applescriptsave4.png

Then the script will be changed into a program. If you run the program it will directly show the alarm time window.

When you run the program it will be shown in the Dock. So if you want to turn the alarm off choose the program and quit.


If you don’t want to listen to music when you sleep, just comment out the portion that turns on the music and turns off the music after 30 minutes by adding “#”. Of course you can also just delete according lines.

If you edit the script a little bit you can listen to different music when you sleep and wake up. Make two iTunes playlists. One for sleeping and one for waking up. Then you can order apple script to play the according playlist. If you google “iTunes applescript” you can find the commands for it.