Last Application not working

Not an expert user. I have enjoyed KM V5 for some time and just went up to V7. Trying a simple spell checking macro. Can’t get It to return to the last app. I don’t have a specific app, I am now writing this in a forum page, sometimes a Facebook post, sometimes a DevonThink Rich text field, sometimes Scrivener, etc.

Once I highlight a section that I want to spell check, I trigger the first KM macro. Trying Bean and TextWrangler. After I am done with proofing the segment, I use the second macro to return the corrected text. Before I do this I am telling KM to close the just opened new document and quite the app I sed to spell check, TextWrangler or Bean.

Problem is when It looks for the last app, It can no longer find It so It errors. Is there a different way to set the name of the app I was in as a variable value and return to It that way?


Hey There,

The macro below shows you how to save the current-app to a variable and then return to it, but I leave it to you to break it up your two tasks.

As for quitting the spell-checking app — these two AppleScripts might come in handy:

Bean is not scriptable but responds to this:

tell application "Bean" to quit without saving

TextWrangler on the other hand is very scriptable.

tell application "TextWrangler"
  close front document without saving
end tell

This script launches TextWrangler, makes a new document named "Spell_Check", resizes the document, and sets its text to the clipboard.

tell application "TextWrangler"
  set newDoc to make new document with properties {text:(get the clipboard), name:"Spell_Check", bounds:{303, 44, 1617, 1196}}
  tell newDoc's window
    if soft wrap text is false then set soft wrap text to true
  end tell
end tell


Generic-Test 01.kmmacros (3.0 KB)

Thanks for the quick reply. Sorry I was pulled elsewhere until I could reply back. I was hoping to use Text Wrangler as It is a stronger platform than Bean. When I recently started using NerdTool to show a task list I keep up with in DevonThink, the ascii wasn’t reading some characters so I zapped the gremlins in TW to fix It. Bean cannot do that.

TW also is a bit frustrating because It opens old windows as a sidebar to the current window. It gets a little confusing to tell TW to open one and one only window as new, paste the text and when done kill that one window - and also quite TW. BBEdit is the same way of course which is why I don’t use It so much anymore. If you can suggest a workaround for this I am all ears.

I also used KM to adjust the wrapping, soft to hard instead of the Applescript you used. Unless I Hard wrap before pasting into TW, TW will put all paragraphs on a single line which is near unreadable. I have to hard wrap to use TW and then soft wrap the results again before I paste results.

Using my Bean example. I start by highlighting my text to spell check and run this KM macro: Type the ⌘C Keystroke Open ‘/Applications/’ Pause Until Conditions Met (Bean to be at Front) Pause for 1.5 Seconds Type the ⌘V Keystroke

Then I spell check and run this macro: Type the ⌘A Keystroke Type the ⌘C Keystroke Filter Clipboard with Unwrap Activate Last Application Pause for 1.5 Seconds Type the ⌘V Keystroke Activate Last Application Type the ⌘W Keystroke Pause for 1.5 Seconds Type the ⌘D Keystroke Pause for 1.5 Seconds Type the ⌘Q Keystroke Activate Last Application

In theory this could work but It leaves me in limbo at the end of It. By switching the order of when I quit Bean or TW I was able to paste the results back successfully. Going back I could quit the editing app. But then It could not find the pasted doc.

I will test using your examples for a benchmark. Thanks very much for suggesting the ideas.

One clarifying question if I may? In your example, are you showing that in KM I can name a variable on the fly? Is “%Application%%1%” a text variable that you essentially made up for this specific task? I have limited programming experience.

Again, many thanks.

UPDATED: All done. Thanks for the help, this works great now.

Except … One small problem, TW is not showing the pasted text with misspelled words red underlined. Yes, spell check is turned on but I see now way to have a pasted block of text recognize the misspelled words so that I can see them and correct them. Bean will do this and other aps will do this. TW should be doing this … ? Sometimes I can force this by highlighting a block of text and right clicking to trigger a replacement search. Not in this case.

Any ideas on this? Thanks again.

Just to clarify, and tie off this thread, I now realize that when I go from any one app to a new app, then quit that new app (so long as I have not gone anywhere else) then the first app is now on top. So really, I don’t need to find a way to return to the first app. By quitting the app I used for spell checking in this case (Bean) I am automatically ready to past results.

Hey Levelbest,

In the 'Application' pane of TextWrangler's Preferences uncheck these:

[  ] Reopen documents that were open at last quit
    [  ] Restore unsaved changes
    [  ] Include documents on servers

Check this one:

When Textwrangler becomes active:
    ( • ) Do nothing

In the following script you can adjust the size (bounds) of the window, and the width of soft wrap to suit yourself.

Misspelled text is highlighted.

Spellcheck as you type is turned ON in case you want to manually edit something and make an error.

# Script 1 — Send clipboard to TW.
tell application "TextWrangler"
  if not running then run
  set newDoc to make new document with properties {name:"Spell_Check", text:(get the clipboard), bounds:{396, 45, 1142, 1196}}
  tell front text window
    select insertion point before its text
    set soft wrap mode to character width
    set soft wrap width to 70
    set soft wrap text to true
  end tell
end tell
tell application "System Events"
  tell application process "TextWrangler"
    set frontmost to true
    tell menu item "Check Spelling as You Type" of menu 1 of menu bar item "Text" of menu bar 1
      if value of attribute "AXMenuItemMarkChar" ≠ "✓" then
      end if
    end tell
    tell menu item "Find All Misspelled Words" of menu 1 of menu bar item "Text" of menu bar 1
    end tell
  end tell
end tell

This will quit TextWrangler without saving the 'Spell_Check' window.

# Script 2 — Quit TextWrangler.
tell application "TextWrangler"
  copy text of front document
  close front document without saving
end tell

As far as I can see these two scripts plus the tweaks to TextWrangler's prefs take care of all of your concerns.

Here are the two complete macros.

The copy macro expects you the user to select the text to be copied and then hit the hotkey.

You then edit the text to your satisfaction in TextWrangler.

You then hit the quit macro hotkey.

Spell-Check-with-TextWrangler.kmmacros (7.4 KB)


Thanks for the update. I will revisit this at some point. As you point out TW is much more robust and I will look harder at this when I come across my next challenge.

Just now I have Bean doing all the work, and It seems to be working well. I just set up a macro that uses some of Beans menus to sort then find and replaces returns for reformatting after the sort. Then paste back to the first document of course.

TW is really a bit too complex for me as a casual user. I am grateful for kind souls like yourself who generously share how It is done using TW. When I paste text into Bean, the misspellings automatically appear, simple. I like simple. :slight_smile:

I just tested TextWrangler to see if I could apply your ideas for what I have ahead of me this afternoon. I have some major editing work to do. Normally I will do my editing work in Bean as this is nothing which requires more than sorting from time to time and some formatting) Unfortunately, opening a document that looks fine in DevonThink and in Bean shows all sorts of characters in TextWrangler.


\b0 \cf0 \expnd0\expndtw0\kerning0


\b \cf2 \expnd0\expndtw0\kerning0[/quote]
I assume that Bean has been acting as a text processor while TextWrangler is more of a text editor which is why TW shows code all over the document and Bean (and DevonThink) do not? I don’t want the code all over the place so, how do I do this using TextWrangler?

Hey levelbest,

You apparently are creating .rtf or .rtfd files in Bean and DevonThink.

TextWrangler is a plain-text editor and will not render RTF. It will only display the raw text of an RTF file.

If you want to bring the text into TextWrangler you'll have to copy from Bean or DT and paste into TW, or save the file from Bean or DT as plain text.


I had to do a little research. The document I am trying to open in TW is rtf. Yes, Devonthink can produce rtf or rtfd files. TW will not open rtfd files because;

TW opens rtf files with added coding all over the document as the example in my OP. I use Word for professional work. I prefer Bean or TW for lighter work, things like this to manipulate macro results.

Can you say a little more please about TW and what you mean by plain-text editor? I used to write in html and I am somewhat familiar with other ways of producing text. Does TW allow colored text or styled text?

TextWrangler will open the RTF file embedded within a .rtfd file, although it takes a bit of fiddling.

However it opens the document as plain-text. Plain-text by definition is not styled-text.

Then you have a basic understanding that styled-text no matter what format — HTML, RTF, PDF, DOCX — is rendered according to some form of schema.

No. Plain-text only.


Thank you Chris. TextWrangler is the same as It’s big sister/brother BBEdit. Very powerful apps. Great for all sorts of serious text manipulation. I respect them both for that but I believe the style question is more important to me than I had previously realized.

Word is way too bloated for this sort of macro. Bean is still useful but It is no longer supported so it is worth looking for future solutions which are also style friendly.

TextWrangler will more than likely be around forever although … Mailsmith was around forever and was eventually dropped by BareBones, the same folks that produce TextWrangler. With software, you never know what tomorrow will bring or what yesterday will leave behind.