Welcome, Guest. Please Login
Tinderbox
  News:
IMPORTANT MESSAGE! This forum has now been replaced by a new forum at http://forum.eastgate.com and no further posting or member registration is allowed. The forum is still accessible via read-only access for reference purposes. If you wish to discuss content here, please use the new forum. N.B. - posting in the new forum requires a fresh registration in the new forum (sorry - member data can't be ported).
  HomeHelpSearchLogin  
 
Pages: 1
Send Topic Print
Bookmarking video/audio in QT Player–AppleScript (Read 7627 times)
Sumner Gerard
Full Member
*
Offline



Posts: 359

Bookmarking video/audio in QT Player–AppleScript
Feb 19th, 2013, 4:06pm
 
Those who don't like doing similar in iTunes can make and play in QuickTime Player "bookmarks" to specific time spots in media by doing something like the following (which illustrates another way of passing values from TB via AppleScript to other apps, suggested by Mark A here).

Step 1. Paste this into the action box of a new stamp named something like PlayQTBmrk and save it:

Code:
runCommand("LC_CTYPE=UTF-8 pbcopy ",$QTFile+"‡"+$TimeOffset); runCommand("osascript -e '" +$Text("PlayQTBmarkAS") + "'") 



Step 2. And paste this into $Text of a code note named PlayQTBmarkAS (name must match name within stamp):

Code:
--retrieve and parse clipboard contents
set tbxval to get the clipboard --get clipboard content
set text item delimiters of AppleScript to "‡" --declare new delimiter
set tbxval to text items of tbxval --parse and coerce to list
set text item delimiters of AppleScript to {""} --restore delimiter
set {path_to_file, start_offset} to {item 1, item 2} of tbxval --assign list elements to variables
--run QT Player using inputs parsed from clipboard
tell application "QuickTime Player"
	open path_to_file -- open the media file
	set my_media to document 1 -- bring it to the forefront (select a document, not a window)
	tell my_media
		set audio volume to 0.1 -- change default volume as desired
		set current time to start_offset
		play
		activate -- make QuickTime Player frontmost
	end tell
end tell 



Step 3. Paste this into a new stamp named something like "Make QTimeBmk":

Code:
runCommand("osascript -e '" +$Text("MakeQTBmarkAS") +"'") 



Step 4. Paste this into $Text of a code note named "MakeQTBmarkAS" (name must match name within stamp):

Code:
tell application "QuickTime Player"
	if exists document 1 then--document 1 is currently active movie or track in QT Player
		set {docName, pathFile, timeOffset} to {name, file, current time} of document 1
		set timeStamp to my secsToHMS(timeOffset)--create a time stamp in hh:mm:ss format:
		--put a tab-delimited header row on the clipboard:
		set the clipboard to "Name" & tab & "QTName" & tab & "QTFile" & tab & "TimeOffset" & return
		--    and then add (the one and only) data row:
		set the clipboard to (the clipboard) & timeStamp & " -" & docName & tab & docName & tab & pathFile & tab & timeOffset & return
		-- alert user that clipboard is ready to paste in Tinderbox:
		tell application "System Events" to display dialog "QuickTime Player bookmark placed successfully on clipboard! … Now click Tinderbox outline view (not an open note) and hit <command-v> to paste." buttons "OK" default button "OK"
	else --if no document is open in QuickTime Player alert user:
		tell application "System Events" to display dialog "Oops! .. First open an audio or video file in Quicktime Player to your desired start time and try again." buttons "OK" default button "OK"
	end if
end tell

on secsToHMS(secs)
--subroutine to convert time in seconds to more human readable form <http://macscripter.net/viewtopic.php?id=27203>
	set secs to secs as integer --cooerce to integer since QT Player (unlike iTunes) stores fractional seconds
	-- to suppress display of hours, delete from the following line:  text 2 through 3 & ":" & "
	tell (1000000 + secs div hours * 10000 + secs mod hours div minutes * 100 + secs mod minutes) as string to return text 2 thru 3 & ":" & text 4 thru 5 & ":" & text 6 thru 7
end secsToHMS 



Done. Now, to create a bookmark when QuickTime is at the desired spot in the audio or video just run the "Make QTimeBmk" stamp and paste the results onto a Tinderbox View (not into an open note).  Move the resulting "bookmark" note wherever, and play it whenever by selecting it and applying the PlayQTBmark stamp.

-------------------------------------------------------------------

Technical notes – not needed for use, but perhaps of general interest

osascript doesn't access stdin, too bad because stdin helps in runCommand() to reduce shell quoting headaches (see all those quotes in the stamp in the iTunes example or the DEVONThink example). An old way to "fake" stdin was send data to stdin (as runCommand() does in its two-argument form) and then access it in AppleScript with this:

   set stdin to do shell script "cat"

But that no longer works in OSX 10.8 (Mountain Lion). Hence the workaround here sending the TBX attribute values (concatenated and delimited by a double dagger character, though the delimiter can be anything not in the attribute values) first to the clipboard with pbcopy (which does take stdin but seems to need LC_CTYPE=UTF-8 to preserve Chinese) and then to the AppleScript via the clipboard.  

Undoubtedly a bit ugly under the hood, but it's easy and reliable on my machine (after getting QT Player to run the first time after upgrading to ML) and has lots of uses for researchers, investigators, writers, etc... Of course if anyone has better ways...
Back to top
 
 
  IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Re: Bookmarking video/audio in QT Player–AppleScript
Reply #1 - Feb 20th, 2013, 5:14am
 
Not sure, but perhaps answers in this stackoverflow thread might help. I suspect the change is a by-product of ever tightening security procedures.

To get TB to show a "Hello world" dialog from an AppleScript in Snow Leopard vs Mountain Lion. I think it breaks down like this…

In OS 10.6.8 I use an AppleScript script:

Code:
set stdin to do shell script "cat"
tell application "AppleScript Runner" to display dialog stdin 


Note, I think you can user "Finder" or System Event" as alternate apps for correctly calling 'display dialog', but you should use something. for me a bare 'display dialog' gave a command line error testing in Terminal.

In TB, I make a Stamp "HW" with the code:

Code:
runCommand('echo "Hello world" | osascript ~/filename.scpt') 



Or, to use text stored in TB (I use $Text but any (coerced-to-)string attribute could be used):

Code:
runCommand("echo '" + $Text("some note") + "' | osascript ~/filename.scpt") 


…where the "Hello word" is the $Text of note "some note". Important: the qoute nesting order in the main runCommand string matters. Use single for the overall string and embed double around the $Text output.  Do it the other way around and you get no result! FWIW, that's the command line syntax biting you, not TB.

I need to shift to a different Mac so I'll post the Mountain Lion version of the solution in a separate post.

[continued in next post…]
Back to top
 
« Last Edit: Feb 20th, 2013, 5:40am by Mark Anderson »  

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v6
(TB consulting - email me)
WWW shoantel   IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Re: Bookmarking video/audio in QT Player–AppleScript
Reply #2 - Feb 20th, 2013, 6:33am
 
In Mountain Lion (10.8.2) the stamp becomes:

Code:
runCommand('myinput="Hello world"; osascript ~/filename.scpt "$myinput"') 


The Applescript script becomes:

Code:
on run argv
	set stdin to (item 1 of argv)
	tell application "AppleScript Runner" to display dialog stdin
end run 


You could just set 'stdin' if 'argv' if you know there's only one input argument. the above works either way so is safe for later when you start adding some complexity to the basic test.

Now shift the input to the $Text of 'some note'. The stamp becomes (after rather longer than envisaged working round TB quote nesting/escaping nightmares!):

Code:
$Text(my input)='myinput="'+$Text(some note)+'"'; runCommand($Text(my input)+'; osascript ~/filename.scpt "$myinput"'); $Text(my input)=;
 


I'm afraid there's no escaping needing to use a note's $Text - as opposed to a normal string attribute.We need to store the exact string myinput="Hello world", including the quotes to avoid having to escape them. However, passing that exact string to a user String attribute results in the closing double quote being clipped off: e.g. myinput="Hello world. Grr! Here's hoping v6 may give us a bit more flex with quote escaping (though I'm sure it's also hard under the hood to address this).

I leave it as an exercise for the reader to move the Applescripts back inside TB if so desired. Otherwise, simply remember that runCommand's working directory is you HD root (/) and not - as you might intuit - your home folder (/Users/[username] or ~). Thus your command line argument should provide a path from root to your script file and not just the script's filename.
Back to top
 
« Last Edit: Feb 20th, 2013, 6:35am by Mark Anderson »  

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v6
(TB consulting - email me)
WWW shoantel   IP Logged
Sumner Gerard
Full Member
*
Offline



Posts: 359

Re: Bookmarking video/audio in QT Player–AppleScript
Reply #3 - Feb 20th, 2013, 3:06pm
 
So, is this what this does?

Reads the "Hello world" value from the $Text of 'some note', encloses that value in double quotes, prepends 'myinput=' to it, and places all that in $Text of a note named 'my input', giving:

    myinput="Hello world"

Then, through runCommand(), it retrieves the value in $Text of 'my input' and sends this to the command line:

    myinput="Hello world";osascript ~filename.scpt "$myinput"

Neat. Great to have an example of how "$var" on the command line works in a Tinderbox context to overcome some of the quoting headaches.

But by using osascript command line arguments rather than stdin (which osascript doesn't recognize but pbcopy does) it seems not all escaping problems are tamed.

For example if the $Text of 'some note' is changed from Hello world to something like:

    "Hello, it's a 'nice' world, " she said. "您好!" he replied.

I get a silent failure on my machine.

Whereas, using stdn-to-pbcopy-to-clipboard in Tinderbox and then access-clipboard-fake-stdin-and-parse in AppleScript does work.

The corresponding stamp for the "Hello World" dialog using that approach would be:

Code:
runCommand("LC_CTYPE=UTF-8 pbcopy ",$Text("some note"));runCommand("osascript ~/filename.scpt") 



Where filename.scpt (placed in my case at "MBA/Users/sumnerg/filename.scpt") would be:

Code:
set stdin to get the clipboard
tell application "System Events" to display dialog stdin 



Perhaps there could be a little more about using Tinderbox with AppleScript in the documentation, covering some of these techniques. For all its faults, AppleScript is useful as a bridge between TB and the rest of the Mac "ecosystem." It's easy to miss (as I did for years) that TB via small AppleScripts can easily call up specific spots within audio/video files, link to specific photos in iPhoto and Aperture, send selected notes to DEVONthink, etc., as well as import AppleScript-generated "custom urls" linking to Bookends, Address Book (now Contacts) entries, etc., etc.
Back to top
 
« Last Edit: Feb 20th, 2013, 4:10pm by Sumner Gerard »  
  IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Re: Bookmarking video/audio in QT Player–AppleScript
Reply #4 - Feb 20th, 2013, 4:33pm
 
The first part of your analysis was correct - I was merely trying to pull a simple generic example from your QT-based process. All was going well until it got to Mountain Lion…!

Documenting AppleScript and runCommand is possible. If someone has time on their hands and want's to help (any lurkers here?) a useful start would be to review the forum for AS-related topics and filet out the actual content, from something could be written up.
Back to top
 
 

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v6
(TB consulting - email me)
WWW shoantel   IP Logged
Sumner Gerard
Full Member
*
Offline



Posts: 359

Re: Bookmarking video/audio in QT Player–AppleScript
Reply #5 - Feb 25th, 2013, 6:23pm
 
Quote:
a useful start would be to review the forum for AS-related topics and filet out the actual content

A forum search for "AppleScript" yielded 86 distinct threads back to 2008. I'm organizing and will post elsewhere a tbx pointing to the most promising content.

One "hit" from 2008, a brief post by Steve Harf, led to an easier way to pass attribute values to AppleScript... and it still works in Mountain Lion!

It turns out that (unlike osascript) the automator command that can be put within runCommand to run Automator will happily accept values from TB via stdin, thus avoiding the shell quoting headaches of passing values to osascript via command line arguments or resorting to the pbcopy clipboard workaround illustrated upthread.

So for the generic "Hello world" example upthread, one would put this in the action of a stamp:

Code:
runCommand("automator -i - ~/filename.workflow",$Text("some note")) 



And this in a Run AppleScript action within Automator:

Code:
on run {input, parameters}
	set msg to item 1 of input
	display dialog msg
	return input
end run 



For the QT Player example, which requires passing two arguments to the player via AppleScript, the Step 1 stamp upthread becomes:

Code:
runCommand("automator -i - ~/play_qt_bmark.workflow",$QTFile+"\n"+$TimeOffset) 



And the Step 2 code (in a workflow file saved from Automator as play_qt_bmark.workflow in the appropriate place) would be:

Code:
on run {input, parameters}
	set path_to_file to item 1 of input --<<$QTFile
	set start_offset to item 2 of input --<<$TimeOffset
	tell application "QuickTime Player"
		open path_to_file as alias -- open the media file
		set my_media to document 1 -- QT document, not window
		tell my_media
			set audio volume to 0.1 -- default volume–change to taste
			set current time to start_offset
			play
		end tell
	end tell
end run 



man automator in Terminal describes usage. The - after -i indicates stdin for input. In the Automator Run AppleScript action input is a list of elements (items) delimited by newlines, hence the "\n" in the stamp.
Back to top
 
« Last Edit: Feb 25th, 2013, 6:29pm by Sumner Gerard »  
  IP Logged
Pages: 1
Send Topic Print