Skip to content

Instantly share code, notes, and snippets.

@ofstudio
Created December 28, 2014 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ofstudio/ab75d15ca2824fb4a85e to your computer and use it in GitHub Desktop.
Save ofstudio/ab75d15ca2824fb4a85e to your computer and use it in GitHub Desktop.
Example of file / folders scripting in OS X with Javascript
/**
* Example of file / folders scripting in OS X with Javascript
* Open /Applications/Script Editor.app, paste the code and click Run button
*
* @author Oleg Fomin <ofstudio@gmail.com>
*
*/
function file_exists(name) {
try {
return Application('System Events').files.byName(name).exists()
} catch (e) {
return false;
}
}
function folder_exists(name) {
try {
return Application('System Events').folders.byName(name).exists()
} catch (e) {
return false;
}
}
file_exists('~/Desktop/exists.txt'); // true
file_exists('~/Desktop/no-exists.txt'); // false
folder_exists('/Library'); // true
folder_exists('/somethingelse'); // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment