Skip to content

ianthekirkland/AppleScript_finderLib

Repository files navigation

AppleScript_finderLib

AppleScript Scripting Library for Common Finder Functions

This library is a collection of common Finder-centric AppleScript subroutines. Many are my own. Many come courtesy of http://applescript.bratis-lover.net. Some of these subroutines (mostly my contributions) are a bit verbose as I wrote them awhile ago before I delved into programming in things such as PHP, Ruby, and Perl. So if portions feel like "three lefts make a right", know that syntactic elegance was not yet in my vocabulary.

Compatibility

macOS / OS X 10.5+ / Works great on Sierra! Developed and/or used on every Mac OS version since Leopard. Uncompiled versions (i.e. with file extension "applescript") included.

Installation

Download or clone the file to ~/Library/Script Libraries. If the Script Libraries folder doesn't exist in your library folder, simply create it.

Usage

Once the file is in the Script Libraries folder, you can use any of the subroutines (or "handlers") in your script by loading this library with the load script command (see below) or the use command.

-- Load library
set _finderLib to load script alias ((path to home folder as text) & "Library:Script Libraries:" & "AppleScript_finderLib.scpt")

Then, you're all set! Call any of the library's functions like this (for example):

tell _finderLib
  set _items to getSelection()
  set _basenames to getBasenamesOfOnlyFiles(_items)
end tell

You can also cut to the chase by nesting functions.

tell _finderLib to set _firstSelectedItemBasename to getBasenameOfItem( getFirstItem( getSelection( )))

For a full list of all the functions, and to demonstrate the above, a starter script in 2 formats, Quick_Start_File (uncompiled) and Quick_Start_File (compiled), is included. It contains a call to every subroutine/function in the library, so you can get a sense of what's available. In addition, all subroutines are listed below:

getSelection( )
--->  {item, item, item, ...}
getOnlyFiles( items )
--->  {file, file, file, ...}
getOnlyFolders( items )
--->  {folder, folder, folder, ...}
getFirstItem( items )
--->  item
getNames( items )
--->  {fullname, fullname, fullname, ...}
getBasenames( items )
--->  {basename, basename, basename, ...}
getBasenameOfFirstItem( items )
--->  basename
getBasenamesOfOnlyFiles( items )
--->  {basename, basename, basename, ...}
getExtensions( items )
--->  {extension, extension, [null], ...}
getExtensionsOfOnlyFiles( items )
--->  {extension, extension, extension, ...}
toggleExtensionsHidden( items )
--->  (shows/hides extensions)
getPosixPaths( items )
--->  {POSIX path, POSIX path, POSIX path, ...}
getPosixPathsQ( items )
--->  {'POSIX path', 'POSIX path', 'POSIX path', ...}
getEscapedPosixArray( items )
--->  "(EscapedPOSIXpath EscapedPOSIXpath EscapedPOSIXpath ...)"
getHFSPaths( items )
--->  {HFS path, HFS path, HFS path, ...}
getHFSPathsQ( items )
--->  {'HFS path', 'HFS path', 'HFS path', ...}
getParentPosix( items )
--->  {parent POSIX path, parent POSIX path, parent POSIX path, ...}
getParentHFS( items )
--->  {parent HFS path, parent HFS path, parent HFS path, ...} 
getBasenameOfItem( item )
--->  basename
getNameAndExt( item )
--->  {name, extension}
getCommentOfItem( item )
--->  comment
getContainerOfItem( item )
--->  parent folder
getPropertiesOfItem( item )
--->  {property:value, property:value, property:value, ...}
getParentAlias( alias )
--->  parent alias
appendBasenamesToParentPOSIX( basenames, parentPOSIX )
--->  {full POSIX path, full POSIX path, full POSIX path, ...}