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" -- haven't checked this works, no access to the relevant computer
end if


tell application "System Events"
        
        try
                myFilePath as alias
                
on error -- this should tell BBEdit to create the file and save it, but it doesn't save it
                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