Tinderbox User-to-User Forum (for formal tech support please email: info@eastgate.com)
Tinderbox Users >> Tinderbox applications >> Linking specific time points in sound, video files

Message started by Sumner Gerard on Sep 17th, 2012, 3:45pm

Title: Linking specific time points in sound, video files
Post by Sumner Gerard on Sep 17th, 2012, 3:45pm

In this thread Mark A described how to use DEVONthink's custom URL scheme to link from a Tinderbox note to a specific time point in a video stored in DEVONthink. That currently works for videos in DEVONthink but not for sound files.

There is a way, not as painful as it sounds, to link from Tinderbox notes to specific time points in both sound files and videos stored in iTunes.

1. Select the desired track in iTunes,
2. Pause at the desired start point
3. Run this script.
4. Paste the contents of the clipboard into a URL attribute in Tinderbox.

Now clicking the button for that URL attribute will call up an Applescript which, when run, will activate the iTunes player (in a small window, which can be maximized to view a video) at the desired time point.

I don't mind a AppleScript Editor window popping up each time I want to hear or see a track. But if that is a distraction, one can avoid it with a little one-time upfront work. Simply copy the script that appears AppleScript Editor the first time after the button next to the relevant Tinderbox URL attribute is clicked, surround the script with single quotation marks, and paste it into a Tinderbox user string attribute.

Thereafter, to call up the video or sound file whenever needed, starting at the right spot, just apply a stamp rather than clicking the URL attribute.  

My stamp is "RunAppleScript" with the following action:

            runCommand("osascript -e"  +$iTunesScript)

... where $iTunesScript is my user string attribute that contains the script copied from the Applescript Editor (duly surrounded by single quotation marks).

Doing it this way the Tinderbox note doesn't need to be opened to activate the link. Just run the stamp from the 'Stamps' menu after selecting the note in the Outline or one of the other major views.

The ability to quite easily "bookmark" in this way makes finding and analyzing material in longish audio and video items a lot easier.  


Improvements/suggestions welcome... Maybe way to just get AppleScript Editor to run automatically and then close, so as not to have to resort to command line stuff? ... Or, if using the command line, way to modify the stamp action code to eliminate the extra step of adding those single quotation marks around the script?

Title: Re: Linking specific time points in sound, video files
Post by Mark Anderson on Sep 18th, 2012, 8:18am

One small suggestion, re the script in step #3 in the list. That script is written one or more selected tracks whereas you downstream process never expects more than one per note. Either never run the linked script with more than one note selected, or initially paste a multi-track selection's output into an notes text and use only one line of it per note, or edit the script as follows…

Remove the repeat block open & close lines (code lines #12 and #21). Replace :
set the clipboard to (the clipboard) & applescriptURL & return
set the clipboard to applescriptURL

An alternate approach would be to only record the track ID and time offset (incidentally latter can only be whole seconds) as a string like "4237EAABC42A6056#2" using a hash (#) as a delimiter. I've successfully tried out an (Automator created) OS service that takes such a string, when selected in Tinderbox, and plays it without showing any AppleScript. In Automator, the service looks like this:

To use I select the text** like so:

And then use TB's Services menu:

You could of course assign a custom shortcut for the service using the the Keyboard Shortcuts pane of Keyboard System Preferences.

** Sadly a selection of a key attribute value isn't seen as a TB text selection (perhaps in TB v6 it could/should).

Whilst this works, I think the inability to use a key attribute value means your runCommand approach is a better one. Otherwise it would be easy to make a service that could easily shared with non-tech users (plus a script to capture the initial track ID and time code).

Title: Re: Linking specific time points in sound, video files
Post by Paul Walters on Sep 18th, 2012, 9:09am

If you only want a single track, then use

set thisTrack to (the first item of current track as list) as record

Get rid of the repeat...end repeat structure, but execute the commands inside of it.

Title: Re: Linking specific time points in sound, video files
Post by Sumner Gerard on Sep 18th, 2012, 11:00am

Thanks much to both for the excellent pointers. I'd wondered about that loop. Over the next few days will experiment and revise script.  And also try the Automator/service approach.

Was thinking that it should be possible (though I don't know if this is any easier) to have TB insert (#-delimited) values for track ID and time offset into script boilerplate (via a macro?) and then run the assembled script via runCommand... taking care to deal with those pesky single quotation marks.

For the occasional link for nontechnical users, just pasting in an entire script, or the two values with a delimiter and then running a Service, may be easier.

Title: Re: Linking specific time points in sound, video files
Post by Sumner Gerard on Sep 18th, 2012, 9:28pm

I took out the repeat...end repeat loop. I don't know enough about coding to get Paul Walters's syntax to work. iTunes seems to expect a reference to an item in a list. But I have had some success with this revised script.

(That just clicking the link above is supposed to load the script into the AppleScript Editor on any Mac helps me understand the rationale for requiring the user to hit the <Run> button rather than letting the script run automatically!).

Meanwhile, I've found that this kind of deep linking to a specific time in a video on YouTube is as easy as appending something like ?t=3m40s to the url pasted into a Tinderbox url type attribute... Apparently not possible with Vimeo... And iTunes seems to be the best (only?) option for audio files.

Title: Re: Linking specific time points in sound, video files
Post by Mark Anderson on Sep 19th, 2012, 3:13am

The list isn't needed, the trip-up is likely because this script was re-purposed from one originally intended to work with multi-track selections. For your use, you really want to work with a single track at a time.

To lose the list which was causing the connfusion, in your latest script delete these lines:

set selectedTracks to current track as list
set thisTrack to the first item of selectedTracks
set {trackPersistentID, trackLocation} to {persistent ID, location} of thisTrack
-- for future use, could add trackName and name to respective lists in line above

Replace them with either of:

set selectedTrack to current track
set trackPersistentID to persistent ID of selectedTrack
set trackLocation to location of selectedTrack


set selectedTrack to current track
tell selectedTrack
   set trackPersistentID to persistent ID
   set trackLocation to location
end tell

The latter might make more sense if you want to record other track properties via the script though they'd not help with the primary task of opening the right track at the right position. That task doesn't need/use the track name, just the track's iTunes ID and the time offset.

Title: Re: Linking specific time points in sound, video files
Post by Paul Walters on Sep 19th, 2012, 5:54am

My last suggested syntax was too verbose.  For parsimony, the simplest script that plays a track starting at a given position is

tell application "iTunes"
     play "Macintosh HD:Music:Tracks:My Track"
     set player position to 50
end tell

a approach to create this simple form of the script is

tell application id "com.apple.iTunes"
     if player position > 0 then
           set the clipboard to "applescript://com.apple.scripteditor2?action=new%26script=tell%20application%20%22iTunes%22%0D%09play20%22" & (location of current track) & "%22%0D%09set%20player%20position%20to%20" & (player position as number) & "%0D%09end%20tell%0D"
           display dialog "Done. Now paste clipboard into Tinderbox URL attribute." buttons "OK" default button "OK"
           display dialog "Oops. Pick the start point you want in a selected iTunes track first, then run this script." buttons "OK" default button "OK"
     end if
end tell

(I hope I got all the character escapement correct in this  ;).  BTW, over here, the pasted URL does not work if ScriptDebugger is the default ApplesScript editor on 10.8.1)

Title: Re: Linking specific time points in sound, video files
Post by Sumner Gerard on Sep 20th, 2012, 11:55pm

Thanks much to both for your help.  Here is an improved script.

For the life of me I couldn't get Paul's script to work on my machine, and it turns out the trick was to figure out how to encode the track location in a form acceptable to the Tinderbox $URL attribute, especially for tracks with Chinese and such in their names.

I've also added here the ability to simply paste the contents of the clipboard onto a Tinderbox view, which automatically brings in not just the script in $URL linking to iTunes, but the track name and time offset in minutes and seconds, etc.

Before figuring out the encoding I was just about to give up and just try to use persistent id. Given a persistent id, I discovered the relevant track can be played thus:

tell application "iTunes"
     play location of item 1 of (file tracks of playlist 1 whose persistent ID is "BCD7FDB0A47364AF") as text
     set player position to 265
end tell

Apparently, though, iTunes sometimes finds (at least it used to sometimes find) more than one track seeming to have the same persistent id. Something about AppleScript's loss of precision.

So using location (properly encoded) seems to be the most reliable way to go, though it breaks the runCommand with osascript approach when there are Chinese characters in the name (seems to work fine with English).

Title: Re: Linking specific time points in sound, video files
Post by Mark Anderson on Sep 21st, 2012, 3:49am

I think, before later readers get confused, that we need to deconflict two issues here - both of which, incidentally, are unlikely to be encountered by the general user and one of which doesn't relate to TB.

1. AppleScript (AS) has an issue where seemingly it doesn't collect a truly accurate unique ID for an iTunes track via it's persistent ID property. This means that - and more likely in very large iTunes libraries - an AS call via persistent ID may occasionally call the wrong track.  There is no workaround though for most users I suspect they'll likely not encounter this issue (otherwise I'd have expected a fix to the iTunes AS support).

2. TB users - such as @Sumner - who TB work involves languages like Chinese** that can only be represented using a full Unicode font in Unicode-capable processes. As is already documented and previously discussed here in the forum, TB 5.x doesn't (and won't) support Unicode in some parts of it's UI. TL;DR summary: TB v5.x doesn't use Unicode in the Create/Rename dialogues, Info view, note key attribute tables). I'd assume the app re-write for v6 (currently underway) will address this in due course.

** so, that's most languages with a non-Roman alphabet, including ancient languages (IIRC we've a user who does much in Aramaic). Noting we've plenty of TB-using authors, the issue also affects fantasy languages such a Klingon (which has a formal Unicode listing!) unless they've written in a Roman alphabet.

Note: the following workaround is needed only if the URL contains Unicode characters.

So, for those users who do use a lot of Unicode-needing language in their TB work and who wish to use this trick of opening an iTunes track via AppleScript? First, on the AS side make sure you're using 'Unicode text' and not 'string' type data; if in doubt specify the data type. Once you have your URL on the clipboard. , make a new temporary TB note.  Set the note's $Rule to set the $URL of the desired note to this note's $Text. For instance, if the target note is called "Q1 Metal Imports", the temporary note's $Rule would be:

$URL("Q1 Metal Imports") = $Text

Then delete the temporary note. In the target note, don't display $URL as a key attribute but instead use the button in the left sidebar to open the target URL.

Of course, this won't can't work with User URL-type attributes as they have no sidebar button. In such circumstances simply make a note per reference and use $URL in each case.

I can't test this workaround right now (not least as I don't understand Chinese) so it might be useful if @Sumner could test and confirm. If using non-Roman alphabet language when doing gnarly AppleScript inter-app work like this, it's best to first test without Chinese and only then only add the latter in, as that way you can flush out the hidden Unicode dependencies.

Title: Re: Linking specific time points in sound, video files
Post by Sumner Gerard on Sep 24th, 2012, 5:16pm

Look forward to not having to worry about languages in v6!  I'll describe my experience with workarounds in a separate thread.

Meanwhile, here's a working script for easily creating "deep links" (essentially bookmarks) to specific time spots in audio and video in iTunes that skirts the language problems:
Download script

Usage details are included in the script file itself. But it's pretty much just: "run, paste, and done".

1) "Run" the script whenever you want to make a bookmark while listening to or watching an and audio or video file in iTunes. (This presupposes, of course, that you brought your audio or video file into iTunes.)

2) "Paste" the results using the usual <command-v> onto an outline view in Tinderbox.

3) "Done." Now, whenever needed, you can play the audio or video from the bookmarked time spot by opening a pasted bookmark note in Tinderbox and clicking the URL icon, then <Run> in the AppleScript Editor window that pops up.

Optional but recommended enhancement that requires quick one-time setup in Tinderbox: Go to the 'Stamps/Edit Stamps' menu in Tinderbox and create a new stamp called "Play iTunes Track' or similar, paste in the provided action code, and hit <Create> or <Change>. Then whenever you want to "roll the tape" simply select the desired bookmark note (which can be open to take notes, but doesn't have to be) and choose the stamp in the 'Stamps' menu.

Here's a sample image of what you end up with in Tinderbox, with little work. (Normally, you wouldn't have the 'Edit Stamp' dialog open. And the time in iTunes is different from the time in the bookmark note because I was slow in grabbing the screenshot)

This kind of easy bookmarking can be useful, I think, for all sorts of Tinderbox general users, including analysts, researchers, writers, language students, investigators, attorneys, and others who need to reference, annotate and make sense of source material in audio and video files containing interview recordings, lecture recordings, voice memos, podcasts, home videos, and the like.

I wonder if other Mac software even exists that allows such easy and flexible bookmarking, annotation and analysis, especially for audio files.

Comments/suggestions/corrections/improvements welcome.

Title: Re: Linking specific time points in sound, video files
Post by Sumner Gerard on Oct 21st, 2012, 9:45pm

A different and better solution that doesn't require "brute force" import of an entire encoded script for each "deep link" (bookmark) and a complicated action in a stamp, but instead passes Tinderbox attribute values to a short AppleScript via a simple stamp action can be found here.

Tinderbox User-to-User Forum (for formal tech support please email: info@eastgate.com) » Powered by YaBB 2.2.1!
YaBB © 2000-2008. All Rights Reserved.