Edit Evernote ENML Applescript

This Applescript just sends the currently selected note to the Evernote ENML Editor. Assign it to a hotkey using your favorite hotkey app and it’s a nice little time saver.


tell application "System Events"
set app_name to name of the first process whose frontmost is true
end tell

if (app_name is "Evernote") then
tell application "Evernote"
set noteList to selection
if (count of noteList) = 1 then
set noteGUID to my get_evernote_note_id((item 1 of noteList)'s note link)
set editURL to "http://enml-editor.ping13.net/note?n=" & noteGUID
end if
end tell
open location editURL

else
tell application "Evernote" to activate
end if

on get_evernote_note_id(this_text)
set AppleScript's text item delimiters to "/"
set the item_list to every text item of this_text
set AppleScript's text item delimiters to ""
return item 8 of item_list
end get_evernote_note_id

iDoneThis Applescript

I’ve been using iDoneThis for a few months now. It’s a simple service that emails you daily and asks “What’d you get done today”. It then keeps track of them all on their website in a calendar. They also mine your data for you to generate graphs and a word cloud. You can also enable daily emails reminding you of what you got one 1 week/1 month/1 year ago. Anyway,the problem with the daily email is that by the time I get the email I forget some things I’ve done. You can add items as you do them by sending an email to today@idonethis.com, but launching mail manually and creating a message is tedious. Thus the simple Applescript below. I have this assigned to a hotkey, but you could also save it as an application (and keep it in your dock).


set doneThis to text returned of ¬
(display dialog ¬
"What'd you do? " with title ¬
"iDoneThis" default answer ¬
"" buttons {"I Done It!", "Cancel"} ¬
default button 1)

if doneThis is not "" then
tell application "Mail"
set MyEmail to make new outgoing message
tell MyEmail
set content to doneThis
set subject to "A new thing I did today"
set sender to "YOUREMAIL@ADDRESS.COM"
make new to recipient at end of to recipients with properties {address:"today@idonethis.com"}
send
end tell
end tell
end if

Update 5/1/12
iDoneThis emailed and asked that I make this in to an application. I’ve made a simple application that’s made to live in your dock. You can quit it if you’d like, or leave it running. Each time you click the icon in the dock/switch to the application it will ask you what you’ve done, you can enter it in and press return or click “I Done It!”, it will then send off the email in the background. If your iDoneThis account isn’t setup using your default Mail email address you can change the email From address by launching/activating the iDoneThis application, then press “Cancel” on the dialog that pops up, then go to the iDoneThis menu select Preferences.

Update 5/23/12
Thanks to James Wang who made a few changes in order to support Teams! I’ve included these changes in the source and application linked below.

Download iDoneThis v1.5

Download Source (Cocoa/Applescript)

Mute Optical Output On OS X

This is truely frustrating. Not being able to control the system volume when using optical output I could definitely deal with, IF I could at least mute it. After some Googling I realized this just wasn’t going to happen – at least not without some creative thinking. This solution is not exactly elegant, but, it does work.

I had recently installed Plex which installed SoundFlower. SoundFlower allows you to send all system output to an application for processing (normally, to record or send audio to another computer/device). I don’t use it at all though, so as I expected activating this effectively muted my audio. If you do use it, well, you may need to figure out another way to do this.

So the first step here is if you don’t have SoundFlower installed, go grab it and install it. Now, I like everything to be controled via the keyboard, but if you don’t care about this then don’t worry about the rest of this post. You can change the audio output via the sound menu in your menubar by holding down option when clicking on it.

If you want to control this with a key command, then here goes.

First download this: SwitchAudioSource. I put it in /usr/local/bin, but thats up to you.

Next I wrote a simple Applescript to execute the command. Applescript editor is located at “/Utilities/Applescript Editor”.

If you plan to use Growl notifications, just run this Applescript first, you only need to run it once to register the notifications we’ll be using.

tell application “GrowlHelperApp”
set appName to “System Preferences”
set notificationName to “Sound Muted”
set notifs to {“Sound Muted”, “Sound Un-Muted”}
register as application appName all notifications notifs default notifications notifs
end tell

Next up, save the following script somewhere:

set curOut to (do shell script “/usr/local/bin/SwitchAudioSource -c”)
if (curOut = “Built-in Output”) then
do shell script “/usr/local/bin/SwitchAudioSource -s ‘Soundflower (2ch)'”
set notificationName to “Sound Muted”
set growlString to “Muted”
my pause_itunes()
else
do shell script “/usr/local/bin/SwitchAudioSource -s ‘Built-in Output'”
set notificationName to “Sound Un-Muted”
set growlString to “Un-Muted”
my play_itunes()
end if

tell application “GrowlHelperApp”
set appName to “System Preferences”
notify with name notificationName application name appName title notificationName description growlString
end tell

on pause_itunes()
try
tell application “Finder” to get (every process whose name is “iTunes”)
if result is not {} then — only update when iTunes running
tell application “iTunes”
if player state is playing then
pause
end if
end tell
end if
end try
end pause_itunes

on play_itunes()
try
tell application “Finder” to get (every process whose name is “iTunes”)
if result is not {} then — only update when iTunes running
tell application “iTunes”
if player state is paused then
play
end if
end tell
end if
end try
end play_itunes

As you can see I added a little more to this. The basic stuff is just the do shell script calls to SwitchAudioSource. I added Growl notifications to tell me when this runs, as well as checking with and pausing/playing iTunes when I mute/unmute.

I run this script with a keyboard command thanks to the excellent application Proxi from Griffin. You could even just save it as an application and keep it in your dock if that suits you.

Disclaimer: There is almost certainly a more elegant way to do this, namely it can almost certainly all be accomplished via just the command line, but getting it working in Applescript was the fastest way for me to do it. This runs in less than a second under heavy load on my system, so, it’s a not a problem for me.

iTunes Sleep Timer – With Fade Out Applescript

I like to listen to music or an audiobook as I fall asleep, but don’t want it to continue playing all night, so I put together this little script to implement an iTunes “Sleep Timer”. It also fades the volume down which makes it very difficult to notice and seems to help falling asleep (could be all in my head though).

How it works: When run it asks how many minutes you want to wait before “going to sleep”. It then waits until “fade_for_minutes” minutes (default is 10) less than the time you specified and starts to fade out iTunes. It pauses after the fade completes, then restores the volume to the setting it was started with.

Example:
Run at: 10:00pm
Sleep time: 60 minutes
fade_for_minutes: 10 minutes
initial volume: 100 (iTunes volume ranges from 0 to 100)

At 10:50 fade_sound will start running. It will decrease the iTunes volume by 1 every 6 seconds ((fade_for_minutes*60)/initialVolume).
At 11:00 it will pause the currently playing track, reset the volume to 100, and quit itself.

You can either try to copy/paste the script below in to Applescript Editor, then save as an application and be sure to check off “Stay Open”, or you can download the Application.


property lastSleepLength : 5
property fade_for_minutes : 10

global endTime, initialVolume

on idle
-- Wait until fade_for_minutes before the end time
-- Then call fade_sound which will fade the sound, pause itunes, and reset the volume
set curTime to current date
if ((curTime + (fade_for_minutes * 60)) ≥ endTime) then
my fade_sound()
quit me
end if
return 1
end idle

on run
try
set endTime to (current date) + getSleepLength()
tell application "iTunes" to set initialVolume to sound volume

on error number -128
-- user canceled
quit me
end try
end run

on fade_sound()
set timeBetweenDecreases to ((fade_for_minutes * 60) / initialVolume)
repeat with i from 0 to initialVolume
delay timeBetweenDecreases
tell application "iTunes" to set the sound volume to (sound volume) - 1
end repeat
my iTunesReset()
end fade_sound

-- Pause iTunes and reset volume
on iTunesReset()
tell application "Finder"
if process "iTunes" exists then
tell application "iTunes"
if player state is playing then
pause
end if
set the sound volume to initialVolume
end tell
end if
end tell
end iTunesReset

-- 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 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

set lastSleepLength to sleepLength

return (sleepLength * 60) -- Return in seconds
end getSleepLength

Restore Previous Display Window Positions AppleScript

This AppleScript is sort of a hack to restore window positions after removing/adding an external display, similar my other script to move all windows to the main screen. You should run it before you disconnect the display, then again when you reconnect it. It’s customized for my environment right now, so you’ll need to look through the code a bit to customize it. I have this saved as an application in my Applications folder for quick launching with Spotlight.

property numFFWindows : 0
property FFPos : {}
property FFSize : {}
property iTunesPos : {}
property iTunesSize : {}
property iCalPos : 0
property iCalSize : 0
property AdiumContactsPos : 0
property AdiumContactsSize : 0
property AdiumIMSize : 0
property AdiumIMPos : 0
property OFPos : 0
property OFSize : 0

display dialog "Set Window Position or Save Window Position?" buttons {"Restore", "Save"} default button "Restore"
set theResult to result

tell application "System Events"
    if (button returned of theResult is "Restore") then
        -- Restore Settings
        if (numFFWindows > 0) then
            tell process "Firefox"
                repeat with i from 1 to numFFWindows
                    set position of window i to (item i of FFPos)
                    set size of window i to (item i of FFSize)
                end repeat
            end tell
        end if
        if (iTunesPos is not {0, 0}) then
            tell process "iTunes"
                set position of window 1 to iTunesPos
                set size of window 1 to iTunesSize
            end tell
        end if
        if (iCalPos is not {0, 0}) then
            tell process "iCal"
                set position of window 1 to iCalPos
                set size of window 1 to iCalSize
            end tell
        end if
        if (OFPos is not {0, 0}) then
            tell process "OmniFocus"
                set position of window 1 to OFPos
                set size of window 1 to OFSize
            end tell
        end if
        if (AdiumContactsPos is not {0, 0}) then
            tell process "Adium"
                set position of window "Contacts" to AdiumContactsPos
                set size of window "Contacts" to AdiumContactsSize
                repeat with i from 1 to (count windows)
                    if ((window i) is not (window "Contacts")) then
                        set position of window i to AdiumIMPos
                        set size of window i to AdiumIMSize
                    end if
                end repeat

            end tell
        end if

    else
        -- Save Settings
        tell process "Firefox"
            set numFFWindows to count windows
            set FFPos to {}
            set FFSize to {}
            repeat with i from 1 to numFFWindows
                set end of FFPos to (position of window i)
                set end of FFSize to (size of window i)
            end repeat
        end tell
        tell process "iTunes"
            set iTunesPos to position of window 1
            set iTunesSize to size of window 1
        end tell
        tell process "iCal"
            set iCalPos to position of window 1
            set iCalSize to size of window 1
        end tell
        tell process "OmniFocus"
            set OFPos to position of window 1
            set OFSize to size of window 1
        end tell
        tell process "Adium"
            set AdiumContactsPos to position of window "Contacts"
            set AdiumContactsSize to size of window "Contacts"
            set AdiumIMPos to {}
            set AdiumIMSize to {}
            repeat with i from 1 to (count windows)
                if ((window i) is not (window "Contacts")) then
                    set AdiumIMPos to (position of window i)
                    set AdiumIMSize to (size of window i)
                end if
            end repeat
        end tell
    end if
end tell

Make Everyday The Middle Of The Week in iCal

I prefer using iCal in “Weekly” view mode. My problem with this mode though is that once you get towards the end of the week, you will not see what is coming up the next day(s) without explicitly switching to the next week – surely we can fix this?

What I wanted was to be able to see today, maybe the day before, and the next 5 days. After all, the past 6 days is seldom useful, but it’s nice to know what is happening the next 3-5 days.

This script will do this for you. It does so by changing the “Start Week On” preference in iCal everyday at midnight.

To reiterate, my preferred setting is to have the SECOND day of the week be TODAY. If you’d prefer another day, change the number 5 in the script below(4 = 1st day, 5 = 2nd day, etc).

The Applescript to perform this is:

-- Quit iCal if it is open
tell application "System Events" to set isiCalOpen to count ¬
	(every process whose creator type is "wrbt")
if (isiCalOpen is 1) then tell application "iCal" to quit

-- Make the change
do shell script ¬
	"defaults write com.apple.ical \"first day of week\" -int " & ((((weekday of (current date)) as integer) + 5) mod 7)
delay 2
-- Open iCal
tell application "iCal" to activate

Open in Script Editor

If you’d like to have this script run everyday, you may want to check out the excellent Launchd editor, Lingon. My LaunchAgent file can be downloaded here: iCal LaunchAgent.

Move All Windows To Your Main Screen…

ApplescriptSo, you’ve got more than one screen? Life is good with all that wonderful screen real estate, then BAM… You end up back with just your main screen, but your applications have left their windows on those other displays, far out of your reach…

Let’s fix that. This little script simply goes and tells all of your applications to get their act together, to move their window’s to the main display.

You’ll need access for assistive devices turned on, which you can do by going in to System Preferences, Universal Access. At the bottom check the box for “Enable access for assistive devices”.

As it is, the script will tell all applications currently running, that if they have a window that is entirely on the main display – to move it to the top left of the screen.

You may have some applications which keep windows off screen on purpose, if you experience any strange behavior with these applications you can exclude them from the applications which get checked by adding their name to the “processesToIgnore” list at the top of the script. There is an example to show how the list should look in the comment right above it. Be sure to use the exact name of the application.

Lastly, if you have more then one display attached when this script is ran it will basically do the same thing, but windows on other screens will not be moved to the main display, only windows outside the range of all current displays.