Tinderbox User-to-User Forum (for formal tech support please email: info@eastgate.com)
http://www.eastgate.com/Tinderbox/forum//YaBB.cgi
Tinderbox Users >> Tinderbox applications >> Clipping to Tinderbox (AppleScript)
http://www.eastgate.com/Tinderbox/forum//YaBB.cgi?num=1271774788

Message started by Mark Anderson on Apr 20th, 2010, 10:46am

Title: Clipping to Tinderbox (AppleScript)
Post by Mark Anderson on Apr 20th, 2010, 10:46am

I was looking for a way to mimic a service to clip text into TB and got as far as the following, which I record here in case it helps others. It is important to understand first that AppleScript can not 'see' content below window level, so the paste occurs in the current window. The latter is unavoidable and placing content more specifically is a non-start for now - so don't ask! The following assumes you've already put some data on the clipboard via script, keystroke, etc.:


Code:
activate application "Tinderbox"
tell application "System Events"
     tell process "Tinderbox"
           keystroke "v" using command down
     end tell
end tell


You do need the opening 'activate' line or else the paste will happen in the current app or at least other than to TB. Indeed if the current TB window is a Text window no paste occurs; so if using the above for real you might want to test the current TB window name begins with "Outline:" or "Map:".

With a major view window the paste occurs in the current window as outline order item #1 of the (current) view if nothing is selected or as the next sibling of the selected note. So, on Map view if nothing is selected the new note goes on the map. If an outline and if nothing is selected the new note is at the root of that outline as the first listed item.

The new note is always called 'untitled' - even if one such already exists. There seems no way to see a note title. You could exploit that insofar as an agent could locate/do things to 'untitled' notes.

The result of the paste is always just text - even if a file has been copied to the clipboard - in the latter case the name of the file is the note's text. Some mark-up survives.  Experiments show that font size, bolding, italics and colour will paste through but things like hyperlinks (in some copied web text), strikethrough and the actual font face don't survive.

Despite all this lack of fidelity in the paste, if you're sitting in some other app with primary focus there and you just want to dump stuff to TB for later sorting out, I think there's some mileage here. I'd just add a line to set activate my 'source' app after the paste so my work focus isn't disturbed too much. Then at the end of your session switch to TB, and go to work on all those untitled notes, or possibly explode one big paste, etc.

Why everything fails when a TB text window is active, I've no idea! Post-edit. this problem was when running from Script Debugger. Running a compiled script, if a Text window is active the active selection is pasted to the end of the current note.

Lastly, I've assumed a TBX is open. Not tested, but but getting Tinderbox to open a TBX via Applescript is likely possible though you'll need to provide the file path - you've no access to the recently used list. Thinks: for the more adventurous GUI AppleScripting might allow you to 'click' a given item on a TB menu.

Title: Re: Clipping to Tinderbox (AppleScript)
Post by Mark Anderson on Apr 19th, 2011, 6:31am

Moving off my old 10.5 Macbook Pro onto a new 10.6 MBP (yes, just as 0.7 due), I noticed the 'Clip to Tinderbox' and 'Clip to Twig' services no longer worked. I'd originally used the ThisService app (from wafflesoft) but their appear to be significant changes to Services under OS 10.6. So I figured it was time to start over, noting that Automator (free with your Mac) can make Services.

So, open Automator (in Applications) and select and select the 'Service' template. Now from the Library's contents (left-most column) choose 'Utilities', and in the next column now select 'Run Applescript'. Drag the latter into the right pane. Now delete the specimen code and paste in this:


Code:
on run {input}
     set currApp to name of current application
     set the clipboard to input as Unicode text
     activate application "Tinderbox"
     if (count of documents of application "Tinderbox") is equal to 0 then
           tell application "System Events"
                 tell process "Tinderbox"
                       -- make a new document
                       keystroke "n" using command down
                        -- needed or the paste event fires before TB is ready
                       delay 0.25
                 end tell
           end tell
           
     end if
     tell application "System Events"
           tell process "Tinderbox"
                 tell window 1
                       -- force cursor to end (in case Text window and $Text present)
                       key code 125 using command down
                       -- paste clipboard data
                       keystroke "v" using command down
                 end tell
           end tell
     end tell
     activate application currApp
end run
Save the workflow - I used "Clip Selection to Tinderbox". Automator automatically enables the new Service so a trip to the Keyboard Shortcuts in System preferences is not needed - unless you want to set a keyboard shortcut.

Process:
  • Current source app must have a test selection
  • Current app's menu, Services sub-menu -> click "Clip Selection to Tinderbox".
  • Source app's selection is copied to the clipboard.
  • Tinderbox is brought to the front (if not opened, it is opened)
  • Service's script checks a TBX is opened, if not one is added (with a deliberate short wait to allow TB the get the new doc in place)
  • Script now pastes the clipboard to the TB window. It is either added as a new note (first sentence of paste forms $Name, whole paste goes to $Text) if in a major view, or if a text window is foremost, the paste is appended at the insertion point.
N.B. when first opened, a Text window with $Text has the cursor insertion point set at the beginning of $Text. This is the reason for added call to Cmd + key #125 (down arrow) - the shortcut sets the cursor to the end of $Text. That seems the safest default for text windows and has no effect in main view.

Helpfully, once you save the service, you can test it live and adjust/edit the script in automator. Neat!

If a URL is the selection, $URL is not set as with a drag/drop. Indeed the title will be the URL to the first full stop, e.g. 'http://www.'. There is currently (v5.8.1) no way to invoke a TB 'drop' action via a service - tat would be quite neat though as it would make the above approach more flexible.

Twig? Well, I'll make a new post for that, this is long enough already.

Title: Re: Clipping to Tinderbox (AppleScript)
Post by Mark Anderson on Apr 19th, 2011, 8:24am

Twig. The Noter view, as a target, is a slight problem. If you just copy the TB service's script and make a similar service for Twig but replacing "Tinderbox" with "Twig" through the script, then you get variable results. TB/Twig doesn't expose (have!) an AppleScript dictionary so interaction is mainly limited to sending shortcuts or driving menus (the latter vulnerable to menu changes over time).

However, you may get unexpected results with Noter view depending on where the cursor is.  There's no simple way to set where the cursor should be.

One option to consider is to add another shortcut call before the paste switch to Outline (Cmd+Opt+O) or Map (Cmd+Opt+M) before the paste call and remove the Cmd+Down-Arrow call. If you do want to past into Noter, you may have better luck if you follow the paste with a code line "key code 36" which sends a Return key press. If the paste has gone into the Noters Name/Search box the extra key-press should make a new note.

Title: Re: Clipping to Tinderbox (AppleScript)
Post by Mark Anderson on Apr 20th, 2011, 6:25am

Exploring with Automator, whilst we can't simulate drag/drop there is a useful file open method to quickly get the data. Set the (Service) workflow to receive 'files of folders' and the 'in' pop-up to 'Finder' (not usre how file selections in other apps would work - feel free to try). Now, from the Library pane's 'Files & Folders' select 'Open Finder Items' and drag it to the workflow. In the 'Open with:' pop-up select 'text files'. Save as service name "Open Data in Tinderbox".

Now in a Finder window, if a text-type file is selected, the Services menu (also on the context menu) will show an "Open Data in Tinderbox" listing.  Click that and the file is opened as a new Tinderbox document. If multiple files are selected each opens a new TBX document. Short of TB adding a 'drop to TB' service/API I don't think it's possible to make a service add a file's data to an already-open TBX. Even so, especially on a small cluttered laptop screen, a service like above is a quick way to push data to TB. Of course, once imported into TB note(s), it's easy to drag the note(s) from the newly spawned 'import' doc to an existing file and delete the former.

Using this method I've found:
  • TXT files make a single note with the filename/ext as the $Name and the contents as text.
  • Tab-delim text sadly doesn't parse as when dropped onto a TBX, so behaves as above.
  • OPML actually creates an outline. Great!
  • I've not tried other formats. Over to you...
OK, this is another workaround and I'm not suggesting these would be as good as a native TB clipping service. However, I don't doubt a well crafted app service isn't trivial to write (and probably isn't everyone's need). Meanwhile, Automator makes it simple for even the tech-averse to make and correctly install an OS service.


Title: Re: Clipping to Tinderbox (AppleScript)
Post by Matt Cawood on Apr 28th, 2011, 3:07am

I've been using a Keyboard Maestro macro triggered by the F4 key that I'm pretty happy with. After copying something to the clipboard, the macro:
- Activates Tinderbox
- Types Cmd-s (saves the current file, just in case)
- Selects the "Outline: <file name>" menu item under the Windows menu (to ensure I get an outline window, not a map or something else.)
- Types "i" ... "n" ... "b" (scrolls to the Inbox container I have at the top of my outline. Dots indicate 0.2 second pauses between keystrokes)
- Types "shift-return" (to create a new note)
... and I take it from there, pasting the clipboard contents into the note that comes up.

Advantage of this method is that all new notes land as children of the Inbox container.

Love to see a home-grown alternative that would do the same, but through a dialogue that allowed prototypes to be set with the keyboard and, optimally, capture URL info if the clip comes from a browser or Mail.

Title: Re: Clipping to Tinderbox (AppleScript)
Post by Mark Anderson on Jul 4th, 2013, 12:56pm

Having moved from OS 10.6 to 10.8, it seems post-Lion Sandboxing has broken Automator created services. The ones I wrote work, but as they now throw a file chooser dialog which you must dismiss the point of the service is lost. It looks like the Keyboard Maestro route is the one to go - clearly Apple doesn't trust us with scripting any more.

Title: Re: Clipping to Tinderbox (AppleScript)
Post by Mark Anderson on Jul 5th, 2013, 7:51am

Now I get to eat my words.  A kind expert on the MacScripter forums, flagged up a hard to guess issue caused by this line of code:


Code:
set currApp to name of current application

It transpires that the AppleScript value current application is always the AppleScript runner environment in which the script is running and not, as often intuited, the frontmost application. So we need to change these lines



Code:
set currApp to name of current application
...
activate application currApp

to:

Code:
tell application "System Events" to set activeApplication to bundle identifier of 1st process whose frontmost is true
...
activate application id activeApplication

Now all just works.

I've thus updated my two services, 'Clip Selection to Tinderbox' and 'Clip Selection to Twig' and which can be downloaded. Each zip has a service file and a read-me explaining use and installation. If you want to re-use code or just explore the AppleScript simply open the service file in Automator.

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.