• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Checking on the existence of a file
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Checking on the existence of a file


  • Subject: Re: Checking on the existence of a file
  • From: Christopher Stone <email@hidden>
  • Date: Thu, 25 Oct 2012 01:01:38 -0500

On Oct 24, 2012, at 11:55, Eric Robertson <email@hidden> wrote:
Instead I decided just to use 'try' and 'on error' when I was producing the alias to the file I wanted to use. This seems to work OK in the following handler:
______________________________________________________________________

Hey Eric,

That is my go-to method for determining if a file exists.

------------------------------------------------------------------------------------------------
on fileExists(_path) # Takes a path as input - HFS, Posix, or ~/Posix
  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 contains ":" then
      alias _path
    else if _path contains "/" then
      alias POSIX file _path
    end if
    return true
  on error
    return false
  end try
end fileExists
------------------------------------------------------------------------------------------------
set f1 to ((path to desktop as text) & "Python Example.scpt")

set _flagHFS to fileExists(f1) # ~ 0.0007 sec

set f2 to POSIX path of ((path to desktop as text) & "Python Example.scpt")

set _flagPosix to fileExists(f2) # ~ 0.0007 sec

set fileName to "Python Example.scpt"
set f3 to "~/Desktop/" & fileName

set _flagPosixUser to fileExists(f3) # ~ 0.001 sec
------------------------------------------------------------------------------------------------

I have a handler like the above in my general.lib.scpt (library) which gets attached to most scripts I write.  I use Script Debugger (commercial), so libraries are attached neatly behind the scenes - but it's easy to do in the Applescript Editor:

------------------------------------------------------------------------------------------------
--» LIBRARIES
------------------------------------------------------------------------------------------------
set genLib to alias ((path to application support from user domain as text) & "Script Debugger 5:Script Libraries:general_lib.scpt")
------------------------------------------------------------------------------------------------
--» MAIN
------------------------------------------------------------------------------------------------
set f1 to ((path to desktop as text) & "Python Example.scpt")
set _flagHFS to genLib's FILE_EXISTS(f1)
------------------------------------------------------------------------------------------------

Speed-testing done with the LapTime.osax.  (Smile with its chrono function is also useful for this.)

I do occasionally use other methods if/when they are more organic to the workflow at hand.

Axel is right.  If you haven't downloaded the ASLG then grab it quick.  It's not the most scintillating read, but it's searchable.  :)

--
Best Regards,
Chris

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

References: 
 >Checking on the existence of a file (From: Eric Robertson <email@hidden>)

  • Prev by Date: Re: Checking on the existence of a file
  • Next by Date: How can i auto install App under ML
  • Previous by thread: Re: Checking on the existence of a file
  • Next by thread: Re: Checking on the existence of a file
  • Index(es):
    • Date
    • Thread