05401, 05403, 05446, 05462, 05482, 05673, 05701, 37signals, 40hz, 2008, 2010, aardman animations, ac propulsion, adium, ads, aim, airport, al franken, algorithm, amazon, andy hertzfeld, animation, apache, apple, applescript, architecture, archive, art, article id, asterisk, at&t, atom, automobile, away message, backpack, badge, barack obama, basecamp, bash, beos, bernie sanders, bicycling, bill atkinson, billboard, blacklisting, blog, blogroll, blogzot, bluetooth, blunt, bluray, book, bookmarklet, bot-net, brad bird, browser, btv, bug, build, bungie, bunny, burlington, call of duty, camera, camping, can-spam, cars, centralized, channel camp, chocolate, classic, classic mac workshop, clipcat, clothing, cms, collection, color classic, comedy, comedy central, comic, computer, concert, conversion, cookbook, corrosion, cowards way out, crack, crashing, creature comforts, criticism, daring fireball, darwin, dashboard, david byrne, dcl, death, delicious, derbi, design, development, digg, dilemma, discussion, disney, domain, download, drivers, dvd, dynetk, e-mail, e3, easter, ebox, eckhart, eckhart köppen, eckhart koppen, eddie izzard, edward gorey, einstein, eject, election, electric motorcycle, electric motorsport, electric vehicle, electronics research laboratory, elmo, emate, emulator, encryption, environment, environmental impact, erin mckeown, escale, exploit, express 530t, expressionengine, feature, feed, feedburner, filtering, finance, firmware, fixdavsvn, flickr, flynn center, focus, font, food, ford, for sale, free, freeverse, freezing, fresh air, frog design, front row, fusion, games, gears of war, geek, geek technique, geocities, git, github, gmail, google, gpr, grammar, grant hutchinson, graylisting, gtd, hack, haiku, halo, hayao miyazaki, health, higher ground, highrise, hiking, hiroshi noguchi, history, hope, hotspot, html, html5, hulu, humor, hybrid, hybrid technologies, hypercard, intel, internet, interview, ipad, iphone, ipod touch, isao takahata, itunes, jabber, japan, javascript, jetblue, jfk, john gruber, john oliver, jon stewart, kid koala, launchd, layover, leopard, liberal, long trail, lorem ipsum, mac, macbook pro, mac mini, macpaint, mac pro, macworld, maczot, magazine, mail, maine, makkintosshu, marathon, marketing, mark hoekstra, matthias melcher, media, mesagepad, messagepad, microbus, microsoft, mobileme, model s, modern warfare, money, monitoring, moon river, motorola, movie, movies, mrtg, multitasking, music, mwsf07, mystic, nascar, ncx, nda, netflix, network, newt/0, newton, newton os, newton press, newtonscript, newtontalk, newton x press, nick park, nitch, npr, on point, openpbx, open source, operation ivy, optimization, organic, os 6, os 8, os9, osheaga, osx, os x, owc, package, palm, password, patch, paul guyot, pbx, pdf, pesticides, photography, pico card, pilot, pixar, playstation, plist, plug-in, pod jungle, politics, productivity, ps3, psp, pump-and-dump, quickdraw, quicksilver, racism, rack-n-roll, radio, ratatouille, rebooting, recycling, regex, regular expressions, remake, required reading, restoration, retrochallenge, review, roadster, room without a window, rss, scion, screencast, script, search, security, server, sesame street, seven days, shame, shelburne, shelburne museum, shirt, shoppinging cart, signature, simon bell, small dog electronics, snow leopard, social, software, solution, sony, spam, spam haus, startup item, statistics, status, stefano paris, stephen colbert, steve jobs, steven colbert, steven frank, studio 360, studio ghibli, subethaedit, subversion, susan kare, swiss, swuser, sync, syndication, sysmon, tablet, tags, tax, technorati, ted talk, television, terry gross, tesla motors, textpatter, textpattern, the colbert report, the daily show, the flaming lips, the gashleycrumb tinies, the radiator, the world, times argus, titles, tkip, todd kollins, tom gage, tools-osx, trailer, trash, travel, tree, trends, troubleshooting, truetype, twitter, typography, tzero, unicel, universal access, unna, update, upgrade, url title, user interface, v710, venue, verizon wireless, vermont, victor rehorst, video, virtualization, vmware, volkswagen, volvo 122, vpr, vw, wait wait don't tell me, wall-e, wallace & gromit, wavelan, web, web 2.0, webkit, web site, whitepaper, wifi, wikipedia, windows, winter warm-up, wireless, wpa, writing, wwdc, wwnc, xbox 360, xbox live, xhtml, xserve, yahoo, ze frank, zero emission

Technorati Chart for 'applescript'

Articles Tagged "applescript":

An AppleScript to Invert the Display in Grayscale (And Another to Revert) ¬

2010-12-17

I’ve been doing a lot of early morning coding in bed, and have found that the only way I can do so without killing my eyes is to select “Use grayscale” as well as “White on Black” in the Universal Access preferences pane in System Preferences. This gives me a far more comfortable, inverted grayscale display. Unfortunately, it’s annoying and painful to initially set when my eyes are at their most sensitive.

I’ve modified a script posted on Apple Discussions to enable grayscale to also invert the display1 and saved it as a run-only application named Invert Display.app:

tell application "System Preferences" to activate
delay 1
tell application "System Events"
	tell process "System Preferences"
		click the menu item "Universal Access" of the menu "View" of menu bar 1
		click the radio button "Seeing" of the first tab group of window "Universal Access"
		if value of (checkbox "Use grayscale" of tab group 1 of window "Universal Access") is 0 then
			click the checkbox "Use grayscale" of tab group 1 of window "Universal Access"
		end if
		-- click the radio button "White on Black" of tab group 1 of window "Universal Access"
	end tell
	key code 28 using {control down, option down, command down}
end tell
tell application "System Preferences" to quit

Naturally, I also created another to restore the settings and named it Restore Display.app:

tell application "System Preferences" to activate
delay 1
tell application "System Events"
	tell process "System Preferences"
		click the menu item "Universal Access" of the menu "View" of menu bar 1
		click the radio button "Seeing" of the first tab group of window "Universal Access"
		if value of (checkbox "Use grayscale" of tab group 1 of window "Universal Access") is 1 then
			click the checkbox "Use grayscale" of tab group 1 of window "Universal Access"
		end if
		-- click the radio button "Black on White" of tab group 1 of window "Universal Access"
	end tell
	key code 28 using {control down, option down, command down}
end tell
tell application "System Preferences" to quit

I just launch them from Spotlight as needed. It’s a little distracting to see System Preferences launch and do it’s thing, but far easier than doing it myself when my eyes are being seared out of their sockets.

1 – Note that I’ve left the line for selecting the “White on Black” radio button in the script, but commented out, in case someone can catch the reason it throws an error. Since I’m in the preference pane anyway, it seems silly to use key code, plus I’d rather be able to verify it’s actually selected (like I do with the checkbox) so I’m not blindly toggling.

An AppleScript to Launch Front Row ¬

2007-08-26

A friend wanted to Launch Front row by pressing the programmable button on his new LaCie D2 Quadra hard drive, but the software wasn’t letting him program a keystroke, so I whipped up the following AppleScript to press Command-Escape for him and so launch Front Row. I’m sure there are plenty of other solutions out there or maybe even this same one, but here it is anyway:

tell application "System Events"
	key code 53 using command down
end tell

Save it as an application (preferably “run only” without a “startup screen”) in your ~/Applications folder if you want to launch it as an app, or in ~/Scripts if you want to launch it from the Script Menu or Quicksilver.

For more information on using the key code AppleScript command, check out the System Events, Key Code and Keystroke article over at Doug’s AppleScripts for iTunes.

Mail's Broken Reply/Redirect/Forward AppleScript Support ¬

2007-08-07

I use the excellent spamtrainer tool on Mac OS X Server mail servers so that I and others can more easily spamassassin what’s spam or not. Up until now I’ve been content to do the following to manually redirect the messages to the junkmail account:

  1. Select the first message in my Junk folder
  2. Cmd-Shift-E (redirect)
  3. “junkm”-Tab (to select my junkmail@domain.tld address)
  4. Cmd-Shift-D (deliver)
  5. Down arrow
  6. If that wasn’t the last message then goto step 2
  7. Else, Cmd-A (select all) and delete/backspace

However, even with spamassassin ripping out most of the spam upfront, there can still be a considerable amount of spam to redirect on Monday mornings and I really don’t need RSI from redirecting spam.

Since I had already tackled some additions to a John Gruber AppleScript a couple weeks ago, I figured I could reasonably solve this problem with an AppleScript. So I whipped out Script Editor, the dictionary for Mail, and the example scripts in /Library/Scripts/Mail\ Scripts/ and started to attack the problem. The only issue: I kept getting “ undefined” errors where the redirect command was supposed to be returning an outgoing message.

Naturally, I figured I must be doing something wrong since I’m not an AppleScripter. After a quick search on macosxhints.com I found an AppleScript to batch-redirect email in Mail.app, but I kept getting the same error with it. I hit up Google, Apple’s AppleScript-users mailing list archives, and MacScripter’s forums and dug out some very unfortunate news: there is “a bug in Mail evidently prevents it from returning a reference (as it should) to a new message created using the forward, redirect or reply commands” and it’s been there for a while (since the release of Tiger).

Verifying the Bug

I had pretty much found all the proof I needed to start getting discouraged, but I had to flesh it out and understand it so as not to be more discouraged… or maybe to further discourage myself? Either way, I developed the following script to test the cases:

using terms from application "Mail"
	on perform mail action with messages selectedMsgs
		tell application "Mail"
			set selCount to (count of selectedMsgs)
			repeat with counter from 1 to selCount
				set msg to item counter of selectedMsgs
				-- try to create a new message
				set newMsg to make new outgoing message with properties {subject:"New AppleScript-genereated Message", visible:true}
				-- try to reply to a message
				set newReplyMsg to reply msg with opening window
				tell newReplyMsg
					set subject to "(AppleScript-generated)" & msg's subject
				end tell
				-- try to forward a message
				set newForwardMsg to forward msg with opening window
				tell newForwardMsg
					set subject to "(AppleScript-generated) " & msg's subject
				end tell
				-- try to redirect a message
				set newRedirectMsg to redirect msg with opening window
				tell newRedirectMsg
					set subject to "(AppleScript-generated) " & msg's subject
				end tell
			end repeat
		end tell
	end perform mail action with messages
end using terms from
-- this is required when _not_ running from the Script menu (e.g. Script Editor, FastScripts, etc.)
using terms from application "Mail"
	on run
		tell application "Mail" to set sel to selection
		tell me to perform mail action with messages (sel)
	end run
end using terms from

If you select one or more messages in Mail and run that script (you can shuffle around the order of creating the reply, forward, or redirect messages if you like) you’ll see the following:

  1. A new, blank message window with the subject “New AppleScript-genereated Message”
  2. A reply to the first message selected
  3. The following error in Script Editor: “The variable newReplyMsg is not defined.”

In Script Editor’s Event Log you’ll see the following:

tell application "Mail"
	get selection
		{message id 768388 of mailbox "Junk" of account "Morgan"}
	make new outgoing message with properties {subject:"New AppleScript-genereated Message", visible:true}
		outgoing message id 248907776
	reply message id 768388 of mailbox "Junk" of account "Morgan" with opening window
		"The variable newReplyMsg is not defined."

The interesting bit is that outgoing message id 248907776 was returned for the call to make new outgoing message with properties, so at least that works correctly.

I wrote the following script to attempt to peek at the id of each outgoing message in the hopes that I might just be able to set the response from redirect/reply/forward manually:

using terms from application "Mail"
	on run
		tell application "Mail"
			repeat with counter from 1 to (count of outgoing messages)
				display dialog "Outgoing message #" & counter & "'s ID #: " & id of item counter of outgoing messages
			end repeat
		end tell
	end run
end using terms from

Apparently Mail considers a message viewer window to be an outgoing message because this script will show the id of the selected message in an open message viewer window. Also, If you leave the new message created with the first script (i.e. the one created by the call to make new outgoing message with properties) open it’ll show the id of that message. However, if you use either a script or manually reply/forward/or redirect a message you’ll get the following error in Script Editor: “Mail got an error: NSReceiverEvaluationScriptError: 3”.

It’s starting to really get interesting, huh!

H3. The Hypothesis

By this point I’ve pretty well confirmed that not only do the reply, forward, and redirect commands not return an outgoing message as they’re supposed to, it appears that they don’t even have ids set. We also know that scripts that relied on these commands worked prior to Mac OS X 10.4 (Tiger).

So what changed in Tiger that might play a part in causing this bug? Well, Apple switched mail from storing all messages for a folder in .mbox format to storing each individual message in their own .emlx file so that Spotlight could more efficiently/easily index those messages.

Is Mail may not generating an id for the new messages because it hasn’t saved them to disk yet? I believe so. And in the case of calling make new outgoing message with properties directly, you’re generating the object right then and there so it gets an id.

Workarounds?

To get around this I can just create a new message and fill in all the requisite properties, right? Almost.

Unfortunately, an outgoing message object contains only a subset of the properties that a message object contains. Most importantly, you have no way to pass the headers from the original message into a new outgoing message which is vital in the case of redirecting messages for training spamassassin as it inspects the full headers. Also, you lose Mail’s tracking of what messages were replied to, forwarded, or redirected, and linking them together (a handy feature to have to give up).

In some cases one can simply make a Rule in Mail to reply, forward, or redirect, but that won’t work in my case. It looks like I’ll have to do this using GUI scripting and have to watch all the messages pop up, fill in, and then disappear.

I do hope this at least helps bring this issue out into the spotlight a little more since it’s existed for so long. I also hope to save someone a lot of wasted effort.

Non-Top-Posting Reply Scripts for Apple Mail (with Signature Support) ¬

2007-07-11

Today John Gruber posted an excellent set1 of AppleScripts that automate replying without top-posting in Apple’s Mail.app. You can read his article for the full description.

It’s exactly what I’ve been looking for to allow myself to keep my anal-retentive e-mail replying habits and increase productivity. However, I had one problem with his implementation: it doesn’t handle signatures gracefully.

I have a couple different e-mail accounts in Mail.app that I use on a daily basis and each has a unique signature (which also helps me know which account I’m replying from), but his script dumps you after the signature if you have one set to be auto-selected. I could just set it to select “None” by default, but then I lose the increased efficiency by having to select the signature.

So, I came up with a four-line addition to his script which adds the following fourth step to his process:

  • If no default or random signature is selected, select the first signature from the pop-up menu.

It is implemented by adding the following immediately before the second-to-last “end tell“:

if value of (pop up button 1 of window 1) is "None" then
	click pop up button 1 of window 1 -- The "Signature" Pop-up
	click menu item 3 of menu 1 of pop up button 1 of window 1 -- The first signature in the list
end if

Of course, one can always change “menu item 3“ to read “menu item ‘signature-name“, where “signature-name“ is the name of the signature you want to select, if you don’t want it to just pick the first out of the list. Hell, you can even modify it to pick a random signature if you want.

So, to be as clear as possible, the full, modifed, AppleScript is as follows:

tell application "Mail" to activate
tell application "System Events"
	tell process "Mail"
		tell menu bar 1
			click menu bar item "Message"'s menu "Message"'s menu item "Reply"
		end tell
		delay 0.5
		key code 117 -- Forward Delete
		key code 125 using command down -- Down Arrow
		key code 36 -- Return
		if value of (pop up button 1 of window 1) is "None" then
			click pop up button 1 of window 1 -- The "Signature" Pop-up
			click menu item 3 of menu 1 of pop up button 1 of window 1 -- The first signature in the list
		end if
	end tell
end tell

Thanks go out to John. Hopefully my modifications will be helpful to others too.

Update: My apologies for the horrendously tiny font for my code blocks. I’ve had a redesign in the works for a while. The site redesign has now gone live, so the code font should be much more legible.

1 I guess you’d call it a set. It’s one script with instructions for the one word addition to make the second script.