Blog with ecto.scpt

Yesterday I talked about blogging from my Palm Zire 21.

Here is the complete AppleScript for Blog with ecto.scpt. Copy this, paste it into Script Editor, and save it to ~/Library/Scripts/Folder Action Scripts/Blog with ecto.scpt

Then right-click on the folder you want monitored - I use ~/Documents/Palm/Matthew Schinckel/NoteTaker/Blog and away you go. For best results, use with MacNoteTaker. Obviously, ecto is required.

All code is ©2004 Matthew Schinckel, but may be freely used with attribution.

Note: the formatting is removed, because ecto wasn’t quite doing it right, but Script Editor will figure it out.

 1on adding folder items to this_folder after receiving added_items
 2 Set this to true to automatically post the item(s)
 3 Leave as false so you can check the data.
 4set autopost to false
 5repeat with each_file in added_items
 6tell application Finder
 7set filename to (name of each_file) as text
 8open for access each_file
 9set entry_body to (read each_file)
10close access each_file
11end tell
13set entry_title to filename
14set date_stamp to word 3 of filename & / & word 2 of filename ¬
15& / & word 1 of filename &   & word 4 of filename
16set entry_date to false
17set entry_date to date date_stamp
18set entry_title to first paragraph of entry_body
19set entry_body to text ((count characters of entry_title) + 2) ¬
20thru end of entry_body
21end try
22–—— You WILL need to change the next line!
23set my_blog to Life According to Matt
24tell application ecto
25set my_doc to make new document at front with properties ¬
26{entry title:entry_title, current blog:my_blog, ¬
27body text:entry_body}
28if false is not entry_date then
29set entry date of my_doc to entry_date
30end if
31if true is autopost then
32do post my_doc
33end if
34end tell
35end repeat
36end adding folder items to

Sharing iTunes Songs

I finally got around to downloading the 10.3.4 OS X update, SharePoints, and Xbox Media Centre (XBMC). My aim for some time has been to share my iTunes library to my Xbox, which is connected up to the TV and Stereo in the lounge.

The MacOS X update provided (apparently) updated Samba sharing, but I still couldn’t get XMP (Xbox Media Player - the previous incarnation of XBMC) to connect to the SMB share I had set up, with all of my MP3s on it. SharePoints fixed that - all I had to do was create a publicly available share.

Setting up XBMC was a breeze, and it even has a cool interface! UnleashX is (almost) banished from my system, and XBMC is my main Dash.

Then I discovered that XCMC can play AAC files! So, I started (and have not yet finished) converting all of my MP3s to AAC. And not just converting, but re-importing, at a bitrate of 128 (small file size, but apparently equivalent to 256 in MP3). See this hint for some tips (and my comments).

I also wanted to re-jig how I had set up sharing my iTunes songs between users - I had a couple of links to the relevant files in each user’s ~/Music/iTunes, and all files were located in ~shared/Music (which all of admin can RW, and all of the world can R). I needed to have seperate Library files, so that we can have our own ratings (I hate Abba, and she isn’t that big a fan of Paul Kelly, for instance). So, I knocked up a script that scanned the ~shared/Music folder and compared any files found to the iTunes LIbrary. If they weren’t in it, it added them.

Here we go:

 1set _tracks to “”
 2tell application iTunes
 3    set sel to tracks of playlist 1
 4    set sel to get a reference to selection
 5    repeat with t in sel
 6        set _tracks to _tracks & 
 7 & (location of t as string)
 8    end repeat
 9end tell
11tell application Finder
12    set _files to “”
13    set _library to Macintosh HD:Users:Shared:Music
14    set _artists to folders of folder _library
15    repeat with _artist in _artists
16        set _albums to folders of _artist
17        repeat with _album in _albums
18            set temp to files of _album
19            repeat with _file in temp
20                set _files to _files & 
21 & _file
22            end repeat
23        end repeat
24    end repeat
25end tell
26set _missing to “”
27repeat with para in paragraphs of _files
28    set para to para as string
29    if para is not in _tracks then
30        set _missing to _missing & 
31 & para
32    end if
33end repeat
35tell application iTunes
36    repeat with para in paragraphs of _missing
37        set para to para as string
38        if para is not “” then
39            set _file to para
40            add _file
41        end if
42    end repeat
43end tell

Which worked, but was a bit slow. So I came up with the following system instead:

Create a file called ~/.last_check

Enter the following into a script, and then run it. (I’ve had to modify some lines to get it to look good - particularly the fp=os.popen… line).

 1#! /usr/bin/env python 
 2# Check for songs newer than ~/.last_check, and add them to iTunes
 4import os
 5import sys
 7fp = os.popen(
 8"find ~shared/Music -name *.m?? -newer ~/.last_check")
10data = fp.readlines()
12if len(data) == 0:
13    print "No New Songs."
14    sys.exit(0)
16for line in data:
17    filename = "Macintosh HD"+line[:-1]
18    filename = filename.replace("/",":")
19    print "Adding", filename
20    os.system('''osascript < <END
21tell application "iTunes"
22add "'''+filename+'''"
23end tell

Should be pretty quick - I only tested it with a few files, but seems to work okay. iTunes is even smart enough to not re-add files that are already there (I think!), so it won’t add duplicates!

My library lives in ~shared/Music, yours may vary!

You will, however, need to use something like Super Remove Dead Tracks if you update one library with a new encoder!

I accept no responsibility if it screws up your iTunes Library file!

Export Artwork and Playlists

Exporting Artwork

Seeing as how XBMC won’t read the id3 tags from AAC files, I had to grab the artwork from each album and place it in a file (folder.jpg/folder.png) inside the album folder. This was actually easier with Windows (aargh!), since iTunes for Windows exports album art as BMP files, which can then be processed. Still, it took me most of the afternoon to do that by hand. I had had a bash at doing it with an AppleScript, but could not work out how to get the data from a <<class PNG/JPEG *>> structure into an image file. Until I came across a script that does that. It was then a trivial matter to make it so that it worked for multiple files, and only processed those files it needed to, and ignored those without artwork. Tip: iTunes will give you an Out Of Memory Error if you try to get the data from a song that has no artworks! (Took me a while to figure this out!) So, the finished script can be found below. I optimised it a bit, but it still takes a while to run, especially if you run it on lots of tracks. Since it’s not something you’ll want to run every day, this shouldn’t be too much of a problem.

Exporting Playlists

I also thought it would be neat to get the playlists I use in iTunes (I use mostly smart playlists, such as 50’s - 00’s Music, Not Played Today, Recently Added and so on), and use them on XBMC. So, I wrote a script that does this, and then ftp’s the resulting files to the xbox. You will need to edit the first few lines: it should be obvious what needs to go where. Anyway, you can download Export Artwork and Export Playlists. You can also download Bracket Change, a small script that swaps the round & square brackets in a track name. (Right-click and choose to download them, else they will just open in your browser window). All of these files should go into ~/Library/iTunes/Scripts (User only) or /Library/iTunes/Scripts (System-wide) for best effect. Note: The first Script is no longer required, since more recent builds of XBMC can handle the data from AAC files.

Sending Files under Adium

I wrote a small AppleScript that sends the currently playing iTunes track to the current chat user in Adium. The only tricky part was that the line:

send the active chat of the first interface controller file filename

requires Adium to be the frontmost application: you cannot test this with Script Editor, only install the code as a an AdiumScript and then run it from within Adium. Since it is usually illegal to copy music, all of the usual disclaimers apply: only use this to send music to which you own the copyright, or is in the public domain. Here’s the full code, if you want the whole thing, email or IM me.

on substitute()     tell application “System Events”         try             get process “iTunes”         on error             return         end try     end tell          tell application “iTunes”         set filename to location of current track     end tell          tell application “Adium”         send the active chat of the first interface controller file filename     end tell          return theChat end substitute

Reconnect all Adium Accounts.

Just a little quickie: sometimes Adium loses connections. This script will reconnect any disconnected accounts.

1    tell application "Adium"
2        repeat with acc in accounts
3            connect acc
4        end repeat
5    end tell

Automation of XHTML Cleanup

I use SubEthaEdit all of the time, and the feature I like just about most is the Copy As XHTML ⇧⌘C. This will create some nice marked up text that accurately represents what SubEthaEdit looks like - it takes your current syntax styling layout and creates is as XHTML. There are a couple of issues, at least with Blogsome. Firstly, for some reason Blogsome needs all style tags inside the block to use ‘, not “. I think it might be to do with the fact it is a <pre> block. I did notice it was screwing with some double-quotes on another script I was posting. My other hassle is that the syntax highlighting isn’t quite right for AppleScript: it doesn’t take into account terms used from applications, so sometimes the wrong colour appear. I will write an Automator action that grabs the XHTML from SubEthaEdit, replaces “ with ‘, and pastes it into the current active text box, I think. I also started using Convert Script To Markup Code - don’t recall where I found this, but it does a much better job of marking up the text from Script Editor, and everything looks much nicer.

Insert iTunes Data into Post

I used to use ecto to paste all of my entries on my blog, but with Blogsome’s XMLRPC issue, I have to use a browser. But it would be nice to automatically get the name and information about my currently playing iTunes track, just like ecto used to do. I wrote an AppleScript that does this, and, inserts the data in at the insertion point for you. Because it’s intended to be run from the Script Menu, and everything run from there runs as “System Events”, I had to hard code in the browser name. If you use another browser, just replace the second line with whatever your browser is called.

 1property star : «data utxt2605» as Unicode text
 2property browser : "Camino"
 3on run {}
 5    tell application "System Events"
 6        try
 7            get process "iTunes"
 8        on error
 9            return "No Track"
10        end try
11    end tell
13    tell application "iTunes"
14        set theTrack to current track
15        set theArtist to artist of theTrack
16        set theAlbum to album of theTrack
17        set theRating to rating of theTrack
18        set theTrack to name of theTrack
19    end tell
21    set theString to "<p class=’itunes’> " & theTrack & " • <a href='" & theArtist & "%22>" & theArtist & "</a> • <a href=’" & theAlbum & "%22>" & theAlbum & "</a> " & myRating(theRating)
22    set the clipboard to theString
24    tell application browser
25        activate
26        tell application "System Events" to keystroke "v" using {command down}
27    end tell
28end run
30on myRating(theRating)
31    set theResult to ""
32    set theTimes to (theRating - 9) / 20 as integer
33    repeat theTimes times
34        set theResult to theResult & star
35    end repeat
36    return theResult
37end myRating

Adium & Camino

I’m a latecomer to Instant Messaging. I’ve only just discovered it, and part of that is because I don’t have many people to talk to over it. I have written one thing though, and that’s an improvement to megalogeek’s %_camino script. You can download it from Adium Xtras: Camino Current Page. Source follows after the jump!

1on substitute()     
2    tell application "Camino"         
3        URL of window 1         
4        set myURL to the result         
5        set myTitle to name of window 1     
6    end tell     
7    return "<HTML><A HREF=\"" & myURL & "\">" & myTitle & "</A></HTML>" 
8end substitute

Age in AppleScript

Someone on the AppleScript Studio Mailing List wanted to know how to get an age; here’s my take:

set birthday to date “Thursday, 1 April 1976 12:00:00 AM” set age to (year of (current date)) - (year of birthday) if month of (current date) < month of birthday then     set age to age - 1 else if month of (current date) = month of birthday then     if day of (current date) < day of birthday then         set age to age - 1     end if end if get age

Adium iTunes Rater

I wrote a quick AdiumScript to rate the currently playing song in iTunes. Trivial. The only downside? Arguments to AdiumScripts need to be typed in using {} rather than (). I’m much faster at typing in () without looking at the keyboard than {}. I guess I’ll have to learn. There are also scripts for quick-rating a track as 0-5 stars: /rate3 will rate a track as ★★★. (That’s three stars if you don’t have Unicode!) /rate{44} will set a rating of two-and-a-bit stars. The AdiumScript bundle includes all of the scripts, and can be downloaded Adium Xtras: Rate Current iTunes Track.