roadrunnertwice: Weedmaster P: "SON OF A DICK. BALL COCKS. NO. FUCKING." (Overcompensating.WeedmasterP - Shitbox)
[personal profile] roadrunnertwice posting in [community profile] macapps
(x-posted from my journal)

So I was just thinking that I missed being able to automatically post the current music to LJ, back when I was using Xjournal. And that got me thinking about whether I could rig something up to just type the current track into the web form for me, right? Answer: Hell yes, I can! It is Applescript, and is predictably barbaric. Check it out:

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:

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.


macapps: Apple logo (Default)
Mac Apps

March 2017

Powered by Dreamwidth Studios

Style Credit

Expand Cut Tags

No cut tags