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
Save Name As ... values to AppleScript and back (Read 9139 times)
Sumner Gerard
Full Member
*
Offline



Posts: 359

Save Name As ... values to AppleScript and back
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:

Code:
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:

Code:
$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.
Back to top
 
 
  IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Re: Save Name As ... values to AppleScript and back
Reply #1 - 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!
Back to top
 
 

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

designer of
Tinderbox

Posts: 2871
Eastgate Systems, Inc.
Re: Save Name As ... values to AppleScript and back
Reply #2 - 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

    $Name=runCommand(pbpaste)

would rename the note based on whatever has been copied to the clipboard.
Back to top
 
 
WWW   IP Logged
Sumner Gerard
Full Member
*
Offline



Posts: 359

Re: Save Name As ... values to AppleScript and back
Reply #3 - 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.
Back to top
 
« Last Edit: Oct 16th, 2012, 5:20pm by Sumner Gerard »  
  IP Logged
Mark Bernstein
YaBB Administrator
*
Offline

designer of
Tinderbox

Posts: 2871
Eastgate Systems, Inc.
Re: Save Name As ... values to AppleScript and back
Reply #4 - 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.
Back to top
 
 
WWW   IP Logged
Sumner Gerard
Full Member
*
Offline



Posts: 359

Re: Save Name As ... values to AppleScript and back
Reply #5 - 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.


Back to top
 
« Last Edit: Oct 17th, 2012, 2:19pm by Sumner Gerard »  
  IP Logged
Pages: 1
Send Topic Print