I've now realised that I am getting a different result if I run the Script from within BBEdit or run it from Applescript Editor. If I run within AE, it behaves as I expect (creates a new file and saves it in the right place). However, if I trigger it in BBEdit via a keystroke, when it detects no file exists already, it only does the first two of the three steps. The pertinent bit is this (but I've left the whole script below). System Events gets involved because of the need for it or Finder to verify whether the file exists or not, this all lives within a BBEdit tell block.

tell application "System Events"
                
                try
                        myFilePath as alias
                        
                on error
                        tell application "BBEdit"
make new text document with properties {contents:"Title: " & myFileName}
                                set source language of text document 1 to 
"Markdown"
save active document of window 1 to file myFilePath -- **when run from within BBEdit, this doesn't happen**
                                
                        end tell
                        
                end try
                
        end tell


Thanks for the input Charlie, btw.

On 22 May 2014, at 23:14, Jason Davies wrote:

Dear BBEdit community

I have been working on a script for hours and I'm sure it's a simple error...but I can't crack it so am throwing myself on your mercy

I use Trunk Notes on iOS. This means there are a bunch of files with CamelCase names in a flat hierarchy in my Dropbox. I want to script BBEdit to create new files from the selection (otherwise it's fiddly, it needs at least a Title: line - Trunk Notes fills in the rest when I edit it there, so that can be left).

So the logic is this (complicated by use on two different computers which give different filepaths)

1) Check which computer I'm on to get the filepath right

2) BBEdit collects the current selection eg TestFile and checks for empty selection

3) The file path is calculated

4) (*this is the bit I can't get to work*) The script checks whether a file already exists -- I don't want to overwrite anything that exists by mistake.

5) If it doesn't, BBEdit creates the file.

What currently happens is that BBEdit goes ahead and creates the new untitled file but doesn't save it. This makes no sense to me as all three actions (create new document; set it to markdown; save it) are all in the same error block.

Grateful for any help on this (thought I had it working then realised I was overwriting files that existed argh).

Cheers



-- sort out which machine it's running from
tell application "Finder" to set sDisk to (name of startup disk)

-- BBEdit gets the selected text and makes it into a file name and then a path
tell application "BBEdit"
        
        set myFileName to selection as string
        if myFileName is "" then
                display dialog "no selection"
                
        end if
        
        if sDisk is "SSD" then
                
set myFilePath to "SSD:Users:ophiochos:Dropbox:trunksync:notes:" & myFileName & ".markdown"
                
        else
set myFilePath to ":Users:ophiochos:Dropbox:trunksync:notes:" & myFileName & ".markdown"
        end if
        
        
        tell application "System Events"
                
                try
                        myFilePath as alias
                        
                on error
                        tell application "BBEdit"
make new text document with properties {contents:"Title: " & myFileName}
                                set source language of text document 1 to 
"Markdown"
                                save active document of window 1 to file 
myFilePath
                                
                        end tell
                        
                end try
                
        end tell
end tell

--
This is the BBEdit Talk public discussion group. If you have a feature request or would like to report a problem, please email
"[email protected]" rather than posting to the group.
Follow @bbedit on Twitter: <http://www.twitter.com/bbedit>

--- You received this message because you are subscribed to the Google Groups "BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].

Reply via email to