On May 23, 2014, at 05:34, Jason Davies <[email protected]> wrote:
> 
> 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
______________________________________________________________________

Hey Jason,

System events is not needed at all for the alias method of file detection.  The 
basic method is thus (encapsulated in a handler):

set _path to 
"Ryoko:Users:myUserName:Dropbox:trunksync:notes:CodeSignature.markdown"

itExists(_path)

on itExists(hfsPath)
  try
    alias hfsPath
    true
  on error
    false
  end try
end itExists

Many people consider this method of file/folder detection to be a hack, but 
it's far faster than asking the Finder or System events (and has been working 
flawlessly for about 20 years).

The proper way to use the Finder or SEV is as follows:

-------------------------------------------------------------------------------------------
set _path to "Ryoko:Users:chris:Dropbox:trunksync:notes:CodeSignature.markdown"

tell application "Finder"
        file _path exists
end tell
-------------------------------------------------------------------------------------------
tell application "System Events"
        file _path exists
end tell
-------------------------------------------------------------------------------------------

Here's a script that does what you want (I believe).

It does not use hard-coded paths but instead detects the local machine's home 
folder and appends the rest of the path from there.

Error-checking is added, and my somewhat more sophisticated file/folder 
detection handler.

-------------------------------------------------------------------------------------------
--» MAIN
-------------------------------------------------------------------------------------------
try
  
  set notesPath to ((path to home folder as text) & "Dropbox:") & 
"trunksync:notes:"
  
  tell application "BBEdit"
    
    tell front text window to set fileName to contents of selection
    
    if fileName ≠ "" then
      set newFilePath to notesPath & fileName & ".markdown"
      
      if my exTant(newFilePath) = false then
        set newDoc to make new text document with properties {contents:"Title: 
" & fileName, bounds:{303, 44, 1617, 1196}}
        
        tell newDoc
          set source language to "Markdown"
          save to newFilePath
        end tell
        
      else
        error "A document with that name already exists!"
      end if
      
    else
      error "No text was selected to create the file name!"
    end if
    
  end tell
  
on error e number n
  stdErr(e, n, true, true) of me
end try

-------------------------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------------------------
on exTant(_path) # Takes a HFS, Posix, or ~/Posix path as input.
  local _path
  try
    if _path starts with "~/" then
      set _path to (POSIX path of (path to home folder as text)) & text 3 thru 
-1 of _path
    end if
    if _path starts with "/" then
      alias POSIX file _path
    else if _path contains ":" then
      alias _path
    end if
    return true
  on error
    return false
  end try
end exTant
-------------------------------------------------------------------------------------------
on stdErr(e, n, beepFlag, ddFlag)
  set e to e & return & return & "Num: " & n
  if beepFlag = true then
    beep
  end if
  if ddFlag = true then
    tell me
      set dDlg to display dialog e with title "ERROR!" buttons {"Cancel", 
"Copy", "OK"} default button "OK"
    end tell
    if button returned of dDlg = "Copy" then set the clipboard to e
  else
    return e
  end if
end stdErr
-------------------------------------------------------------------------------------------

If you run into any problems let me know.

--
Best Regards,
Chris

-- 
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