Tinderbox User-to-User Forum (for formal tech support please email: info@eastgate.com)
Tinderbox Users >> Agent, Actions, Rules & Automation >> Save Name As ... values to AppleScript and back

Message started by Sumner Gerard on Oct 15th, 2012, 10:14pm

Title: Save Name As ... values to AppleScript and back
Post by Sumner Gerard on Oct 15th, 2012, 10:14pm

DEVONthink has a homely little contextual menu item "Save Title As".  All it does is set the title of the record to the text selected within the record. Nothing special. But it does the deed with one click. No cutting and pasting. I've missed it in Tinderbox when working with lots exploded notes I want to quickly rename, usually to words or phrases somewhere in their respective texts.

My new friend AppleScript to the rescue. On my machine all that's needed is a script like this:

on run
     set myStampName to "_clp2nam" --name of Tinderbox stamp
     activate application "Tinderbox" -- make Tinderbox frontmost
     delay 0.2 --Give Tinderbox a little time to get set
     tell application "System Events" to tell process "Tinderbox"
           set the clipboard to (keystroke "c" using command down)
           click menu item myStampName of menu "Stamps" of menu bar item "Stamps" of menu bar 1
     end tell
end run

And a stamp (here with unfriendly name "_clp2nam" to remind me it's not usually run from the Stamps menu) with this action:

$Name=runCommand("osascript -e 'the clipboard as Unicode text' ")

If the script is pasted into a Run AppleScript action in the Automator (pick "Service" when Automator asks for document type and then drag Run AppleScript to the right panel and paste over the suggested script) it looks like this:

Then through System Preferences/Keyboard the service can be assigned a keyboard shortcut, like this:

Or the script can be saved in a Scripts folder and called from a scripts menu. On my machine it will run directly via FastScripts and can be assigned a keyboard shortcut there.  Without FastScripts, it will still run (without keyboard shortcut) as long as it is saved as an "Application", not as the default "Script".  

Haven't figured out why saving as an Application works and as a Script doesn't, or why, unlike many AppleScripts, it doesn't behave well when run from a code note using runCommand() with osascript.

But it's been handy... and, for me, an interesting early success in passing values from Tinderbox to AppleScript and back.

Title: Re: Save Name As ... values to AppleScript and back
Post by Mark Anderson on Oct 16th, 2012, 1:21pm

Ghis seems like massive overkill. If I understand the task is to set $Name to currently selected text (presumably part/all of $Text of the current note).

For the current note this is simply done via:
  • shortcut Cmd+C  (copy current selection to the keyboard).
  • shortcut Enter (open Rename dialogue for current note/agent/adornment).
  • shortcut Cmd+V (Rename opens with $Name already selected).
  • shortcut Return (close Rename and commit changes).
Wouldn't a keyboard macro applet or something like QuickSilver be a much more appropriate way to achieve your end? Or, make an Automator service for TB and take the current $Text selection as input then pass the above shortcut sequence.

I think I've understood the process you're after, if not sorry!

Title: Re: Save Name As ... values to AppleScript and back
Post by Mark Bernstein on Oct 16th, 2012, 3:09pm

I think the issue with using this script from a Stamp may be the following:

a) A stamp is activated by making a menu choice
b) The script that's run by the stamp wants to make menu choices itself. But, as far as Mac OS X is concerned, it's still handling the Stamp menu.

That might not be the problem, but it's a theory.

Let me call to your attention a useful command. pbpaste takes whatever is on the clipboard and writes it to the standard output. So, something like


would rename the note based on whatever has been copied to the clipboard.

Title: Re: Save Name As ... values to AppleScript and back
Post by Sumner Gerard on Oct 16th, 2012, 5:20pm

Thanks for the helpful responses.

@Mark A --

The process I'm "after" is precisely the process I described and the process that already occurs. It is also what happens in DEVONthink (which I think you have):  select text in a note, hit a keyboard shortcut or make a contextual menu choice, and … that's it, no more steps, you're done! 

No need to command-c, enter to open a dialog, find the right spot, command-v, return to close a dialog, find your place again, etc.  Long-time users may have that sequence in muscle memory. But for me, if there's "massive overkill" somewhere–and I'm not sure there is–that's where it would be. I'd rather have the machine do that purely mechanical stuff for me.

DEVONthink's interface certainly isn't perfect. But it does have this one little touch right.  And now Tinderbox does too, without taking valuable development time away from more difficult things. That's one of things AppleScript is good for, isn't it?

As presented above, this already is an Automator Service and a simple one at that: five lines of code within a run handler, and one brief line of code in a stamp.  A snap to set up and add a keyboard shortcut... Note I didn't take the current $Text selection as input in the Service as you suggest because I couldn't get it to pass Chinese. The gui-scripted command-c works nicely, though. And there's a bonus: it works not just with $Text but with text selected in key attributes too.

@Mark B --

Thanks for the tip on pbpaste. I actually managed to stumble across pbpaste and pbcopy last week and had pbpaste in an early version of my stamp code. Short and snappy, but I had to abandon it after discovering a huge shortcoming, on my machine at least: it doesn't work with Chinese and presumably other double-byte languages.  Then I settled on the only slightly more verbose $Name=runCommand("osascript -e 'the clipboard as Unicode text' "), which does exactly the same thing (or so I think) but handles Chinese!

I think your theory about why running a script with gui scripting from a stamp doesn't work is probably right. Either that or osascript remains frontmost even after an "activate Tinderbox" statement in the script. I experimented with AppleScript's "path to me" and that's what it seemed to indicate... No idea why selecting from the menu when saved in a scripts folder as an "application" works but as a "script" doesn't. Or why FastScripts works either way.  Anyway, it works!  And my experience so far is that running an AppleScript with osascript in a stamp with runCommand works well, whether the script is in a code note, a service, or in a script menu--unless there's gui-scripting, in which case code notes are definitely to be avoided.

Title: Re: Save Name As ... values to AppleScript and back
Post by Mark Bernstein on Oct 16th, 2012, 7:05pm

The man page for pbpaste indicates that you can set an environmental variable to request utf8. Absent that extra step, I'm not sure what the default setting will be (and that default will probably differ depending on the main language selected for your user account).

For those watching at home -- I repeat this in part to get in straight in my own head -- we're now working an issue that arises:

   - only when running an action from a stamp (which is fairly rare)
   - only when that action needs to access data from the clipboard
   - only when that data requires utf-8, but the main language used
       by that Macintosh doesn't. For example, a system whose main
       language is English but where the clipboard data is Chinese.

Once we understand what you need to do, I'm confident that we'll find a much easier way to do it than this elaborate GUI Scripting approach. But this is not something I can easily undertake with a fever and disordered GI system.

Title: Re: Save Name As ... values to AppleScript and back
Post by Sumner Gerard on Oct 17th, 2012, 2:18pm

No worries. Please get well. The (very simple, really) script and one line of action code already do what I need to do, just as in DEVONthink.  

Just so you know, I run an action from a stamp all the time.  In fact that is all I do with stamps.  I can't live without the Stamps menu. Please keep it!

There seem to be a lot of people using Macs these days (including, I suspect, prospective Tinderbox users) who routinely work with multiple languages, including Chinese. I'm no longer a rare case.

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.