This is my Applescript icon.
Aug. 13th, 2011 08:28 pm![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
Paste current iTunes track.scpt:
tell application "iTunes"
copy (a reference to (current track)) to current_track
set track_string to artist of current_track & " โ " & name of current_track
end tell
set the clipboard to ((track_string) as text)
tell application "System Events" to keystroke "v" using command down
(I was originally going to just use the keystroke function to actually TYPE the text, instead of blowing away the clipboard, but this turns out to result in comedy for tracks with any non-ASCII characters.)
And then when I was about to post about how cool this was, I ran smack into how annoying it is to turn formatted RTF-ey text into a useful HTML fragment. So I wrote a script to do THAT for me, too. This one requires an external tool called "premailer," so you'll have to install that with gem.
This one SHOULD work with styled text copied from Word, TextEdit, or basically anywhere, but I haven't tested it very thoroughly yet.
Convert RTF clipboard to HTML fragment.scpt:
-- YOU MUST install premailer for this whole process to work. Just run "sudo gem install premailer" in your terminal.
-- If you're ever debugging this crap, you can run "clipboard info" as an applescript to see all the formats on the current clipboard. For whatever reason, the pbpaste | textutil combo hates it if there's more than one format and will blow up, even if you use -Prefer rtf. IDEK.
set dataRTF to the clipboard as ยซclass RTF ยป --I don't know what the hell those brackets around class RTF mean. They came from here: http://forums.omnigroup.com/showthread.php?t=18784
set the clipboard to dataRTF
--Note a single double-escaped backslash in that perl script.
--Also note that since we're not running in Terminal, apparently we don't get a sensible value for $LANG, which pbcopy and pbpaste need in order to not crap mojibake all over the place. So we have to export it ourselves.
do shell script "export LANG=en_US.UTF-8; pbpaste -Prefer rtf | textutil -convert html -stdin -stdout -format rtf | premailer | perl -e 'my $body = 0; while (<>) { $body = 0 if /^<\\/body>/; print if $body; $body = 1 if /^<body>/; }' | pbcopy"
As ever, just paste these into Applescript Editor, save them in ~/Library/scripts, and run them with FastScripts.