Auto-update iChat status, auto-upload to website

  1. Greg Hurrell

    Tomorrow (4 March 2005) Synergy 1.7 will be released to the public. One of the features of the new version is the capability to run scripts and launch items whenever the iTunes track or status changes (requires iTunes 4.7 or later).

    The way it works is simple: on each change any items found in "~/Library/Application Support/Synergy/Track Change Items/" are launched. The main intention behind this new feature is that people will use AppleScripts (or similar) to do all sorts of useful things, and further automate and tie together their iTunes/Synergy experience. This introduces a large degree of flexibility and extensibility.

    Here are some of the possible applications for which this new feature could be used:

    1. iChat status update script

    An AppleScript that changes your iChat status line to reflect the track you're currently listening to. Because Synergy doesn't poll iTunes, you can add this integration in right now without wasting CPU resources and without waiting for Tiger (it's a rumoured feature of Tiger).

    2. Auto-update of web page or blog

    An AppleScript that can update your webpage or blog to show what track you're currently playing.

    3. Song logging

    A script to maintain a log of the songs you've heard.

    4. Let your imagination run wild...

    Basically, the sky is the limit...

    In version 1.7 the implementation is very simple. The items in the Track Change Items folder are launched when something notable happens, and its their responsibility to decide what information they need and then act upon it. For example, an AppleScript might do something like 'tell application "iTunes" to get current track' if the track name is the desired piece of information. It can then do whatever necessary with the info (upload to a website, send it to iChat etc).

    In the future I intend to pass some launch arguments to the Track Change Items (basic information like title, artist, album, and so forth) so that they don't have to query iTunes. For the time being, however, I just wanted to see what ideas people could come up with. If enough people make submissions, I'll set up an area on the site where you can download people's contributed scripts (much as I've done with the button set gallery).

    So please let me know what ideas you have. I'm definitely not an AppleScript expert, so I'll be interested to see what some of the more wizened veterans will be able to come up with. Put those thinking caps on, and check back here tomorrow for the 1.7 release.

  2. iJosh

    I think this feature is excellent, but giving it a quick test writting a script and finding out it has to be compiled as an application. On every track change the script app, or how many ever scripts/applications/aliases/documents I had in the Track Change Items folder would appear in the dock and take focus away from the current application. It seems it would be more useful if Synergy could process scripts like the script menu in any application handles scripts; or possibly handle applescripts in the background. I am alomst positive that Cocoa has a function for this exact thing? I am no applescript whiz myself, just getting started with the language, so I am not sure if there is something I am not doing right? Anyone watching this board know of a way to keep the launched Script app off the dock and to run in the background? But thanks for giving the users such a powerful event to script. 8')

  3. cazlar

    Quote: Anyone watching this board know of a way to keep the launched Script app off the dock and to run in the background?

    I'm not sure if this will do what you want, but you can add the following lines to an apps Info.plist so it doesn't appear in the Dock etc:

    <key>NSUIElement</key> <string>1</string>

  4. Greg Hurrell

    Quote: It seems it would be more useful if Synergy could process scripts like the script menu in any application handles scripts; or possibly handle applescripts in the background. I am alomst positive that Cocoa has a function for this exact thing?

    This is something worth considering. It's true that Cocoa can "run" AppleScripts. The way Synergy currently implements the feature is that it performs the equivalent of a double-click on the Track Change Items. But this could be changed. Keep the suggestions coming.

  5. iJosh

    Quote:

    I'm not sure if this will do what you want, but you can add the following lines to an apps Info.plist so it doesn't appear in the Dock etc:

    <key>NSUIElement</key> <string>1</string>

    Unfortunately a compiled applescript .app isn't a package and doesn't have it's own info.plist. Now if I took this into applescript studio and made it a litte more than what it should be. Perhaps this could be done. But thank you for that tid bit. I was wondering if the same trick used with iPulse was vaid with other applicaions.

  6. ckd

    Quote: Unfortunately a compiled applescript .app isn't a package and doesn't have it's own info.plist.

    Script Editor will let you save as an Application Bundle (or a Script Bundle). If you save as an Application Bundle, it gets an Info.plist.

    Click on the app bundle, do a "Show Package Contents" from the gear menu or control-click or right-click.

    Open Info.plist -- it'll bring up the Property List Editor. Add a key "LSBackgroundOnly", type Boolean, value Yes. (This makes it a totally faceless background app.)

    Then drop the app into the Track Change Items folder, restart Synergy so it notices, and you're set!

    I used a hacked-up copy of the "Now Playing in iChat" script by Doug Adams, with the idle and quit handlers removed, and the run handler taking all the work the idle handler had been doing. I don't want to distribute it just yet (since I'd want to get Doug's permission first), but it's easy enough to write your own anyway.

  7. Greg Hurrell

    Quote: I used a hacked-up copy of the "Now Playing in iChat" script by Doug Adams, with the idle and quit handlers removed, and the run handler taking all the work the idle handler had been doing. I don't want to distribute it just yet (since I'd want to get Doug's permission first), but it's easy enough to write your own anyway.

    Once you're happy with your work and are sure that you've got rights to redistribute it I'd love to see it.

  8. iJosh

    Quote: Script Editor will let you save as an Application Bundle (or a Script Bundle). If you save as an Application Bundle, it gets an Info.plist.

    Click on the app bundle, do a "Show Package Contents" from the gear menu or control-click or right-click.

    Open Info.plist -- it'll bring up the Property List Editor. Add a key "LSBackgroundOnly", type Boolean, value Yes. (This makes it a totally faceless background app.)

    Thanks for this, it works beautifully, as soon as I figured out to save the app bundle script as run only. Now I will work on making this something anyone can use instead of just me.

    I still think it would be nice for Synergy to pass off OSA scripts to the proper handlers using Cocoa, it just seems to make more sense. I am not sure who is going to want to launch a document/non-script application on every track change event.

  9. ckd

    Quote: I am not sure who is going to want to launch a document/non-script application on every track change event.

    A Unix script or program, perhaps? I can see that. Though you could wrap it in an OSA "do shell script" I suppose.

  10. Greg Hurrell

    Quote: I am not sure who is going to want to launch a document/non-script application on every track change event.

    I think you're right that it would be nice to handles scripts "internally" instead of doing a "plain" launch. As for why people would want to launch a document/non-script thing, I just wanted it to be as flexible as possible, so people could write stuff as shell scripts, Apple scripts, or even applications.

    The main things I had in mind when adding the feature were:

    1. iChat status updates 2. Website ("now playing") updates 3. Song logging (to file)

    But I'm sure people have other ideas...

    If we can get a library of examples together, I'll include them in a "Sample Track Change Items" folder on the Synergy disk image, and put them up on the website too.

  11. glenn mcdonald

    I had some fun playing with this, too. As you predicted, I started with iChat and a now-playing upload, as I'd been wanting to get rid of iChatStatus and OnDeck for simplicity. By hacking together borrowed pieces from elsewhere, I got the basic thing running really quickly. Sanding off the corners took a little longer: getting proper Unicode escaping for Japanese text in the upload (which OnDeck never handled), remembering to check for iTunes running at the beginning so my script didn't immediately reload iTunes every time I quit the program. That, plus I kept getting distracted by other little ideas that occurred to me as I was doing this, so by the end I'd also added some Folder Actions to my system, written several other iTunes scripts and fiddled with a bunch of perl stuff. And learned more than I wanted to about Unicode.

  12. Greg Hurrell

    Here's one example of an auto-upload script for a website. Not specifically designed for use with Synergy, but it would work.

    http://jokke.dk/news/index.php?id=166

  13. Greg Hurrell

    Found some more interesting links for scripts that could be used as a starting point:

    http://www.macosxhints.com/article.php?story=20030825110446769

    http://george.hotelling.net/90percent/os_x/itunes_cover_art_ichat_icon.php

    http://www.cocoaobjects.com/applescript/index.php

    http://www.malcolmadams.com/itunes/scripts/scripts03.shtml#nowplayinginichat

  14. Greg Hurrell

    And another link that you might be able to use to come up with scripts:

    http://www.macosxhints.com/article.php?story=2005030907364616

  15. neilio
    I've managed to put together a PHP script and Applescript combo which updates my personal site with the currently playing track. It also stores the last 50 tracks which were played and outputs them in a separate page.

    You can see both in action here:

    http://www.beatnikpad.com/playlist.php

    First of all, I would like to add my request for Synergy to automatically run Applescripts without having to compile them as applications.

    The other problem I'm having is that I can't seem to get my applescript to run when the track changes. My script is inside of the Synergy Application Support folder, inside of a folder called "Track Change Items". I've saved it as an application bundle in Script Editor, but it still doesn't run.

    If I double-click on the application bundle, my script executes as expected.

    Any ideas on what I could be doing wrong? I'm running Synergy 1.7.1.

    Thanks!

    Neil P.S. There's a problem with the forum style sheet - all of the post text for all of the entries is center-aligned in Firefox.

  16. neilio

    Sorry, I figured it out - I didn't realize you needed to stop and start Synergy for it to see any new scripts in the Track Change Items folder.

    One last question, then - does this script only run when the actual track has changed? Or if I start and stop iTunes and the same track is played twice will Synergy run the script twice, also?

    I just want to know if I need to build in some code to check if the new track is a duplicate of the previously uploaded one.

    Thanks!

  17. Greg Hurrell

    Quote: Sorry, I figured it out - I didn't realize you needed to stop and start Synergy for it to see any new scripts in the Track Change Items folder.

    Good point; I should probably try to document this better, although I do already make mention of it here:

    https://wincent.dev/a/products/synergy-classic/features/

    And in the change log for the version where the feature was introduced:

    https://wincent.dev/a/products/synergy-classic/history/#1.7

    Once I get some user-contributed scripts I'll make a dedicated page for them on the website, and that's probably where the instructions should go for making your own scripts as well as a reminder about the need to stop and restart Synergy for the scripts folder to get re-scanned.

    Synergy Advance will have a similar feature, and I plan to add a Scripts menu as well as a menu item to "Refresh" the menu.

    Quote: One last question, then - does this script only run when the actual track has changed? Or if I start and stop iTunes and the same track is played twice will Synergy run the script twice, also?

    You get notification of status changes too, not just track changes. This is so you can update information like "Playing" or "Paused" if you want.

  18. Greg Hurrell

    Quote: You can see both in action here:

    http://www.beatnikpad.com/playlist.php

    I can't reach the site right now, but let me know once you've got a final version up. I'd love to see how you've done it.

    Quote: P.S. There's a problem with the forum style sheet - all of the post text for all of the entries is center-aligned in Firefox.

    Thanks for pointing this out. I use Safari and so hadn't noticed. I am going to look into it right now and see if I can find out what's wrong.

  19. Greg Hurrell

    Ok, I believe I've fixed the style sheet. Tested in Safari and Firefox and seems to display fine in both.

    Don't know why, but the site-wide style sheet had "text-align: center;" specified on the "body" tag.

    Let me know if you see any breakage, either here or on the rest of the website.

  20. neilio

    Okay, after some finangling I seem to have my Applescript / PHP combo working fairly well. The biggest hurdle was dealing with character encoding issues with tracks that have accents in them. I was originally using a couple of the character conversion subroutines from Adriaan Tijsseling's open source Kung-Tunes code, but it was very, very slow, and pinned the CPU at 100% usage.

    I finally figured out that I could pipe the stuff I was sending by HTTP post through one of the OS's installed languages, and found some Python code that seems to work quite well.

    The link to the results has been working now for about three or four days:

    http://www.beatnikpad.com/playlist.php

    I am having some issues, though. Because the script is triggered on quit, on pause, etc., I needed to implement a basic check to remove the possibility of duplicate items - I write the previously posted data to a text file, and then compare the new data to that before sending it to the site.

    The biggest issue I'm having right now is I can't figure out how to only run the bulk of the script if iTunes is actually playing. It would be great if Synergy passed along this information to the script, along with the current playing track's info!

    I tried using:

    if not (player state is playing) then return

    But then the script doesn't fire at all, even if iTunes is playing.

    Does anyone have an idea how to accomplish this? Once I get this working, and once I clean up and abstract some of the file paths, I can post this somewhere for folks to use.

  21. Greg Hurrell

    Quote: The link to the results has been working now for about three or four days:

    http://www.beatnikpad.com/playlist.php

    Wow. Looks great!

    Quote: I tried using:

    if not (player state is playing) then return

    But then the script doesn't fire at all, even if iTunes is playing.

    I think something like this should work, shouldn't it?... Code: if player state is playing then

       -- do stuff
     end if
  22. neilio

    Okay! I think I got it working. Once I've fixed some variables so it'll run anywhere, do you want me to send this to you, Wincent?

    I do have one more request - it would be nice if there was a way to toggle whether or not Synergy actually runs the scripts or not. I can see times when I wouldn't want the music I'm listening to appear as my iChat status, for example, and right now the only way to deactivate this feature is to physically move the script out of the Track Change Items folder.

    Having this tied to a quick key and also in the Synergy menubar menu would be perfect - could this be added?

  23. Greg Hurrell

    Sure send it in!

    Both of the things you request are already slated for Synergy Advance. It's doubtful that they'll make it into Synergy Classic because it's already suffering from a bit of UI cramp. I've made a few entries in the bug tracking database so the requests doesn't get overlooked...

    https://wincent.dev/a/support/bugs/show_bug.cgi?id=169 https://wincent.dev/a/support/bugs/show_bug.cgi?id=170 https://wincent.dev/a/support/bugs/show_bug.cgi?id=171 https://wincent.dev/a/support/bugs/show_bug.cgi?id=172

  24. glenn mcdonald

    I'm often skipping around in my Library looking at things while music is playing, with the result that my music sometimes just stops at the end of the current track because I've unintentionally moved the selection. So I've got a "Resume" script that looks at the last track in Recently Played, and if it wasn't the last track of an album, plays the following track in that album.

    But I still have to trigger this script manually, so I'm trying to hook it into a Track Change script. This is easy enough, but I haven't been able to figure out a way to tell the difference, in script, between 1) the case where the player state is "stopped" because the last track finished, and 2) the case where the player state is "stopped" because I hit the Stop button myself in the middle of a song. Obviously in the latter case I don't want iTunes to immediately start playing again.

    Any ideas?

  25. glenn mcdonald

    It's not exactly what I was asking, but I thought of a solution to the underlying problem that seems to be working OK. Whenever a new track starts, my Track Change script puts it into a "last started" playlist (and deletes anything else there). iTunes adds tracks to "Recently Played" when they finish, not when they start. Thus if the last track in "Recently Played" is the same as the track in "last started", it means that track played from start to finish. If so, my script starts the next track from the same album, if available. If not, it means I stopped playback myself, so the script does nothing.

  26. Greg Hurrell

    Clever stuff. Good to hear you worked it out.

    Still haven't received any scripts from anyone though... Whenever you're ready you can send them in and I'll make a special area on the site for them.

    On another note, I've made some progress over the last 24 hours towards implementing those requested features. I hope I can have it ready by the time Synergy Advance comes out on 2 May.

  27. glenn mcdonald

    I just mailed you a possible sample script with the Resume thing I was working on above.

  28. monkeymojo

    All Synergy needs to do is perform like Freshly Squeezed Software's Recent Tunes. That is a GREAT app for getting your info online, but it works very inconsistently in Tiger. Besides, I'd rather run one app than two, and I always have Synergy running. I would love to see a solution/addition similar to Recent Tunes.

  29. Greg Hurrell

    Quote: All Synergy needs to do is perform like Freshly Squeezed Software's Recent Tunes. That is a GREAT app for getting your info online, but it works very inconsistently in Tiger. Besides, I'd rather run one app than two, and I always have Synergy running. I would love to see a solution/addition similar to Recent Tunes.

    There is a feature request open for this in the database here:

    https://wincent.dev/a/support/bugs/show_bug.cgi?id=230

    I'm currently looking for people to add comments to that request so that I can figure out exactly what people are looking for, so please add your input there if you can.

  30. neilio

    Sorry I haven't sent this yet - I haven't forgotten! I've been really busy and needed to find some spare time to abstract the applescript and PHP so it would work anywhere.

    I need to put together some basic instructions and I'll package this up and send it to you, Wincent. Hopefully in a few days...

    Neil

Reply

This topic is now closed.