AppleScript - open current file in BBEdit in other application

968 views
Skip to first unread message

TJ Luoma

unread,
Jul 2, 2011, 4:33:33 PM7/2/11
to BBEdit Talk


{Let me preface this by saying that I don't know jack about
AppleScript. I've been googling around (and even checked the manual)
and can't find anything like this, so I'm guessing it's so basic no
one thinks anyone has to be told how to do this.}

Problem: I want to open the front-most file in BBEdit in another
application by pressing some keyboard command.

Let's say the other application is TextEdit, but it could be any
application.

On page 322 of the manual (see, I read it :-) it says:

> Although BBEdit still supports the “active document” property, this is no longer necessary. Instead, if a text window is frontmost:
>
> document 1 of application "BBEdit"
> document 1 of text window 1 of application "BBEdit"
> active document of text window 1 of application "BBEdit"
>
> now all refer to the same document.

So, cobbling together that with other stuff from around the web (lots
of which is old and confusing)

I tried this:

tell application "BBEdit"
set thefile to document 1 of application "BBEdit"
set thepath to POSIX path of thefile
end tell

do shell script "open -e " & thepath

I also tried it without the tell / end tell lines

But I always get an error such as this:

https://skitch.com/luomat/f8had/bbedit-as-error

"BBEdit got an error: Can't make POSIX path of text document into type
POSIX path of text document 1."

which tells me absolutely nothing helpful.

Can someone:

1. point me in the right direction?

2. recommend a good "AppleScript for beginners" book?

Kendall Conrad

unread,
Jul 2, 2011, 8:10:41 PM7/2/11
to BBEdit Talk
Your attempt is close. Here's a script that is working for me. You cn
comment out the "do shell" line with -- at the beginning of the line
and uncomment the line above it to see the file path that it will try
to open for testing.

tell application "BBEdit"
set _file to file of front window
--display dialog "File: " & quoted form of (POSIX path of _file)
do shell script "open -a TextEdit " & quoted form of (POSIX path of
_file)
end tell


-Kendall

Rich Siegel

unread,
Jul 2, 2011, 11:39:26 PM7/2/11
to bbe...@googlegroups.com
On Saturday, July 2, 2011, TJ Luoma <luo...@gmail.com> wrote:

>Problem: I want to open the front-most file in BBEdit in another
>application by pressing some keyboard command.
>
>Let's say the other application is TextEdit, but it could be any
>application.

In that case, you'll use:

tell application "TextEdit"

open <foo>

end tell

>set thefile to document 1 of application "BBEdit"

A document has properties, one of which is its file -- documents
don't equate to files on disk, though. So, "<foo>" in the above becomes:

file of document 1 of application "BBEdit"

Thus:

tell application "TextEdit"

open file of document 1 of application "BBEdit"

end tell

>2. recommend a good "AppleScript for beginners" book?

<http://www.barebones.com/support/resources/bookshelf.html>

Enjoy,

R.
--
Rich Siegel Bare Bones Software, Inc.
<sie...@barebones.com> <http://www.barebones.com/>

Someday I'll look back on all this and laugh... until they
sedate me.

Christopher Stone

unread,
Jul 2, 2011, 9:11:19 PM7/2/11
to bbe...@googlegroups.com
Hey TJ,

On Jul 02, 2011, at 15:33, TJ Luoma wrote:
> Problem: I want to open the front-most file in BBEdit in another application by pressing some keyboard command.
>
> Let's say the other application is TextEdit, but it could be any application.

Simple. (See script below.)

> do shell script "open -e " & thepath
>

> But I always get an error such as this:
>

> "BBEdit got an error: Can't make POSIX path of text document into type POSIX path of text document 1."


Let's start by fixing your script:

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


tell application "BBEdit"
set thefile to document 1 of application "BBEdit"

set thepath to file of thefile --> returns an alias
set thepath to POSIX path of thepath --> properly coerce alias to posix path
end tell

# Since Mac paths can contain spaces and characters that Posix doesn't like we quote the string:
set thepath to quoted form of thepath

do shell script "open -e " & thepath

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

Now let's do it with pure Applescript and provide some error checking:

------------------------------------------------------------------------------------------------
try
tell application "BBEdit"
if front window exists then
if class of front window is not in {shell window, scratchpad_window} then
tell front text document of front window
if its file ≠ missing value then
set frontTextDoc to its file as alias
else
error "The Front Text Document Is Unsaved!"
end if
end tell
else
error "Front Window Is NOT A Text Document!"
end if
else
error "No Windows Are Open!"
end if
end tell

tell application "TextEdit"
activate
open frontTextDoc
end tell

on error errMsg number errNum
set sep to "=============================="
set e to sep & return & "Error: " & errMsg & return & sep & return ¬
& "Error Number: " & errNum & return & sep
beep
display dialog e
end try
------------------------------------------------------------------------------------------------

For my own use I usually won't put in so much error checking and will just let the generic error-handler catch whatever comes up.

Hmm. A good beginner book...

Let's start out with some freebies:

The Applescript Language Guide:

http://developer.apple.com/library/mac/#documentation/AppleScript/Conceptual/AppleScriptLangGuide/introduction/ASLR_intro.html

http://developer.apple.com/library/mac/documentation/AppleScript/Conceptual/AppleScriptLangGuide/AppleScriptLanguageGuide.pdf

Old but worth having:

AppleScript Finder Guide - January 05, 1994:

http://manuals.info.apple.com/en_US/AppleScriptFinderGuide.pdf

AppleScript Scripting Additions Guide - January 05, 1996:

http://manuals.info.apple.com/en_US/0305098AASADDGENG.pdf

Applescript For Absolute Starters v1.4:

http://files.macscripter.net/sourcebook/AS4ASb2.pdf

--

The Applescript Users List (must join and lurk and ask questions):

http://lists.apple.com/mailman/listinfo/applescript-users

MacScripter:

http://macscripter.net/

--

Books I own:

"Learn Applescript: The Comprehensive Guide to Scripting and Automation on Mac OS X 3rd Edition" by Hamish Sanderson & Hanaan Rosenthol

"Applescript: The Definitive Guide 2nd Edition" by Matt Neuburg

"Applescript: Developer Reference" by Mark Conway Munro

If you only buy one I'd probably recommend the first one by Hamish and Hanaan.

I'm also interested in:

"Apple Training Series: AppleScript 1-2-3" by Sal Soghoian & Bill Cheeseman

I'm interested in this one because Bill is involved, and I've known him online for about 17 years.

Now then - I'll bet you got more than you bargained for. :)

Drop me a line if you have any questions.

--
Best Regards,
Chris

Christopher Stone

unread,
Jul 2, 2011, 11:33:25 PM7/2/11
to bbe...@googlegroups.com
This hasn't posted to the list in more than 2 hours, so I'm sending again.
______________________________________________________________________

Hey TJ,

On Jul 02, 2011, at 15:33, TJ Luoma wrote:

> Problem: I want to open the front-most file in BBEdit in another application by pressing some keyboard command.
>
> Let's say the other application is TextEdit, but it could be any application.

Simple. (See script below.)

> do shell script "open -e " & thepath
>

> But I always get an error such as this:
>

> "BBEdit got an error: Can't make POSIX path of text document into type POSIX path of text document 1."

Let's start by fixing your script:

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


tell application "BBEdit"
set thefile to document 1 of application "BBEdit"

set thepath to file of thefile --> returns an alias
set thepath to POSIX path of thepath --> properly coerce alias to posix path
end tell

# Since Mac paths can contain spaces and characters that Posix doesn't like we quote the string:
set thepath to quoted form of thepath

do shell script "open -e " & thepath

TJ Luoma

unread,
Jul 3, 2011, 1:39:07 PM7/3/11
to BBEdit Talk

Thanks to everyone for the replies.

TjL

Reply all
Reply to author
Forward
0 new messages