Basic file system functions for Applescript and JXA

FWIW I’ve put parallel sets of some basic file system functions in the AppleScript and JavaScript sections of this Rosetta Code page:

1 Like

Thanks for sharing, Rob. I’m sure those will be very useful. :+1:

FWIW, here is an AppleScript that checks both files and folders using just one handler.
It is based on a script written by @ccstone and @ShaneStanley.

###AppleScript doesItemExist()

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

doesItemExist("~/Documents") --> true
doesItemExist("~/BadFolder") --> false
doesItemExist("~/Documents/A Real File.txt") --> true
doesItemExist("~/Documents/A File that does not exist.txt") --> false

on doesItemExist(pPosixPath) -- @Path @File @Finder @ASObjC
  (*  VER: 1.0    2017-03-02
  PURPOSE:  Determine if the Path Actually Exists
    • pPOSIXPath    | text  | POSIX Path to check
  RETURNS:  boolean │  true IF the file/folder does exist; else false
  AUTHOR:  JMichaelTX
  BASED ON:  Script by Chris Stone & Shane Stanley
    1. Does a NSURL Have a Valid Target
  ##  Requires:  use framework "Foundation"
  local myNSURL, doesItExistBool
  --- Expand tilde (~) in Path (if it exists) ---
  set pPosixPath to (current application's NSString's stringWithString:pPosixPath)'s stringByExpandingTildeInPath
  set myNSURL to current application's |NSURL|'s fileURLWithPath:pPosixPath
  set doesItExistBool to (myNSURL's checkResourceIsReachableAndReturnError:(missing value)) as boolean
  return doesItExistBool
end doesItemExist
--~~~~~~~~~~~~~~~ END OF handler doesItemExist ~~~~~~~~~~~~~~~~~~~~~~~~~

I’m sure it could be converted to JXA easily.

The doesFileExist functions in AS and JS report on the existence of both files and folders.

The doesDirectoryExists function just adds a second check – that the entity both exists and is also a folder.

Thanks for clarifying. That was not obvious from any of your posts/comments. You might want to add that as a comment in the script.

A good way to get some intuition for those two functions is probably just to look at the commented test results (in line and at the end of the code).

(Each function is shown applied to two files and two folders, and the results for each are listed)

One way to back that up with some complementary intuition may be to observe that both are wrappers for .fileExistsAtPath. The only difference is that one adds the additional isDirectory test.

Inferring behavior from one or two examples can be dangerous and wrong. Problem is solved by a simple comment. Why not add it?

Its behaviour is most reliably documented by the test results, which are focally displayed.

(Never worth wasting time blaming others for any of our failures to understand. Always better to use the time experimenting and testing :slight_smile:

Experiment is the only source of knowledge about anything.

Nullius in verba.

and in particular, never believe documentation or aspirant experts : -)