-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppleScript_finderLib.applescript
More file actions
897 lines (723 loc) · 22.4 KB
/
AppleScript_finderLib.applescript
File metadata and controls
897 lines (723 loc) · 22.4 KB
1
---------------------------------------------- FINDER LIBRARY ------------------------------------------------ Description: This file is a library of common Finder functions-- Purpose: By loading this library into your script, any of the functions below become available to it-- How to Use: Load this library by calling it within your script per the example below-- Example: (Assumes that this library file is at '~/Library/Script Libraries/AppleScript_FinderLib.scpt')(* set _finderLib to load script alias ¬ ((path to home folder as text) & "Library:Script Libraries:" & "AppleScript_finderLib.scpt") tell _FinderLib set _result to functionName(_input, _param, _anyOtherParams) end tell*)-------------------------------------------------------------property myName : "_FinderLib" ---> (the name of this library)--------------------------------------------------------------------------------------------------------- LIST OF FUNCTIONS --------------------------------------------(*getSelection( )=> {item, item, item, ...}getOnlyFiles( items )=> {file, file, file, ...}getOnlyFolders( items )=> {folder, folder, folder, ...}getFirstItem( items )=> itemgetNames( items )=> {fullname, fullname, fullname, ...}getBasenames( items )=> {basename, basename, basename, ...}getBasenameOfFirstItem( items )=> basenamegetBasenamesOfOnlyFiles( 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 )=> basenamegetNameAndExt( item )=> {name, extension}getCommentOfItem( item )=> commentgetContainerOfItem( item )=> parent foldergetPropertiesOfItem( item )=> {property:value, property:value, property:value, ...}getParentAlias( alias )=> parent aliasappendBasenamesToParentPOSIX( basenames, parentPOSIX )=> {full POSIX path, full POSIX path, full POSIX path, ...}*)------------------------------------------------ FUNCTIONS ----------------------------------------------------- getSelection( ) ------- Description: Gets every item of a selection-- Input: Finder selection-- Parameters: (none)-- Result: List of references to Finder itemson getSelection() tell application "Finder" set _sel to the selection set _items to every item of _sel end tell return _itemsend getSelection----- getOnlyFiles( items ) ------- Description: Gets only the non-folder / non-alias files from Finder items-- Input: Finder items-- Parameters: (none)-- Result: List of references to filtered Finder itemson getOnlyFiles(_items) tell application "Finder" set _list to {} repeat with _item in _items if kind of _item is not "Folder" and kind of _item is not "Alias" then set end of _list to _item as alias end if end repeat end tell return _listend getOnlyFiles----- getOnlyFolders( items ) ------- Description: Gets only the folders from a set of Finder items-- Input: Finder items-- Parameters: (none)-- Result: List of references to Finder folderson getOnlyFolders(_items) tell application "Finder" set _folders to {} repeat with _item in _items if kind of _item is "Folder" then set end of _folders to _item as alias end if end repeat end tell return _foldersend getOnlyFolders----- getFirstItem( items ) ------- Description: Get the first item of the Finder items ( items )-- Input: Finder items-- Result: first itemon getFirstItem(_items) if _items is not {} then try tell application "Finder" repeat with _item in _items set _item to item 1 of _items end repeat end tell on error _msg number _n beep display dialog ¬ "Oops! Something went wrong: " & {return} & {return} & ¬ _msg & {return} & "( error code " & _n & " )" with title ("Error") with icon (caution) return end try end if return _itemend getFirstItem----- getNames( items ) ------- Description: Get list of selected items' fullnames with extensions ( items )-- Input: Finder items-- Result: List of nameson getNames(_items) if _items is not {} then try set _list to {} tell application "Finder" repeat with _item in _items set _fullname to name of _item set end of _list to _fullname end repeat end tell on error _msg number _n beep display dialog ¬ "Oops! Something went wrong: " & {return} & {return} & ¬ _msg & {return} & "( error code " & _n & " )" with title ("Error") with icon (caution) return end try end if return _listend getNames----- getBasenames( items ) ------- Description: Get list of items' names ( items )-- Input: Finder items-- Result: List of basenameson getBasenames(_items) if _items is not {} then try set _list to {} tell application "Finder" repeat with _item in _items set _fullname to name of _item if name extension of _item is not "" then set _here to -(offset of "." in ((reverse of text items of _fullname) as text)) - 1 set _basename to (text 1 thru _here of _fullname) set _basename to (_basename as string) else set _basename to _fullname end if set end of _list to _basename end repeat end tell on error _msg number _n beep display dialog ¬ "Oops! Something went wrong: " & {return} & {return} & ¬ _msg & {return} & "( error code " & _n & " )" with title ("Error") with icon (caution) return end try end if return _listend getBasenames----- getBasenameOfFirstItem( items ) ------- Description: Get name of the first selected items ( items )-- Input: Finder items-- Result: basename of first itemon getBasenameOfFirstItem(_items) if _items is not {} then try tell application "Finder" repeat with _item in _items set _item to item 1 of _items set _fullname to name of _item if name extension of _item is not "" then set _here to -(offset of "." in ((reverse of text items of _fullname) as text)) - 1 set _basename to (text 1 thru _here of _fullname) set _basename to (_basename as string) else set _basename to _fullname end if end repeat end tell on error _msg number _n beep display dialog ¬ "Oops! Something went wrong: " & {return} & {return} & ¬ _msg & {return} & "( error code " & _n & " )" with title ("Error") with icon (caution) return end try end if return _basenameend getBasenameOfFirstItem----- getBasenamesOfOnlyFiles( items ) ------- Description: Get list of non-folder / non-alias filenames-- Input: Finder items-- Result: List of basenames of files (not including folders and aliases)on getBasenamesOfOnlyFiles(_items) if _items is not {} then try set _list to {} tell application "Finder" repeat with _item in _items if kind of _item is not "Folder" and kind of _item is not "Alias" then set _fullname to name of _item if name extension of _item is not "" then set _here to -(offset of "." in ((reverse of text items of _fullname) as text)) - 1 set _basename to (text 1 thru _here of _fullname) set _basename to (_basename as string) set end of _list to _basename end if end if end repeat end tell on error _msg number _n beep display dialog ¬ "Oops! Something went wrong: " & {return} & {return} & ¬ _msg & {return} & "( error code " & _n & " )" with title ("Error") with icon (caution) return end try end if return _listend getBasenamesOfOnlyFiles----- getExtensions( items ) ------- Description: Get list of extensions (folders and extension-less items return null)-- Input: Finder items-- Result: List of extensions of fileson getExtensions(_items) if _items is not {} then try set _list to {} tell application "Finder" repeat with _item in _items set _ext to name extension of _item set end of _list to _ext end repeat end tell on error _msg number _n beep display dialog ¬ "Oops! Something went wrong: " & {return} & {return} & ¬ _msg & {return} & "( error code " & _n & " )" with title ("Error") with icon (caution) return end try end if return _listend getExtensions----- getExtensionsOfOnlyFiles( items ) ------- Description: Get list of non-folder / non-alias extensions-- Input: Finder items-- Result: List of extensions of files (not including folders and aliases)on getExtensionsOfOnlyFiles(_items) if _items is not {} then try set _list to {} tell application "Finder" repeat with _item in _items if kind of _item is not "Folder" and kind of _item is not "Alias" then if name extension of _item is not "" then set _ext to name extension of _item set end of _list to _ext end if end if end repeat end tell on error _msg number _n beep display dialog ¬ "Oops! Something went wrong: " & {return} & {return} & ¬ _msg & {return} & "( error code " & _n & " )" with title ("Error") with icon (caution) return end try end if return _listend getExtensionsOfOnlyFiles----------------------- show / hide extension ------------------------- Description: Toggle the visibility of Finder item extensions ( items )-- Input: Finder item(s)-- Result: Extensions shown or hidden (returns boolean true on success)on toggleExtensionsHidden(_items) tell application "System Events" tell application "Finder" repeat with _item in _items set _item to _item if name extension of _item is not "" then set _extHidden to extension hidden of _item if _extHidden is false then tell _item to set extension hidden to true else tell _item to set extension hidden to false end if end if end repeat end tell end tell return trueend toggleExtensionsHidden----- getPosixPaths( items ) ------- Description: Get list of items' POSIX paths ( items )-- Input: Finder items-- Result: List of POSIX pathson getPosixPaths(_items) if _items is not {} then try set _list to {} tell application "Finder" repeat with _item in _items set _path to (POSIX path of (_item as alias)) set end of _list to _path end repeat end tell return _list on error _msg number _n beep display dialog ¬ "Couldn't get POSIX path: " & {return} & {return} & ¬ _msg & {return} & "( error code " & _n & " )" with title ("Error") with icon (caution) end try end ifend getPosixPaths----- getPosixPathsQ( items ) ------- Description: Get list of items' quoted POSIX paths-- Input: Finder items-- Result: List of quoted form of POSIX pathson getPosixPathsQ(_items) if _items is not {} then try set _list to {} tell application "Finder" repeat with _item in _items set _path to (POSIX path of (_item as alias)) set end of _list to (quoted form of _path) end repeat end tell return _list on error _msg number _n beep display dialog ¬ "Couldn't get quoted POSIX path: " & {return} & {return} & ¬ _msg & {return} & "( error code " & _n & " )" with title ("Error") with icon (caution) end try end ifend getPosixPathsQ----- getEscapedPosixArray( items ) ------- Description: Get shell array of items' escaped POSIX paths-- Input: Finder items-- Result: String of [Shell array of escaped form of POSIX paths]on getEscapedPosixArray(_items) tell application "Finder" set _list to {} set original_delims to AppleScript's text item delimiters repeat with _item in _items set _path to (POSIX path of (_item as alias)) set _path to (_path as text) set _pathString to {} set AppleScript's text item delimiters to " " set _escapedSpace to ("\\ " as text) repeat with _t in every text item in _path set end of _pathString to ((_t & _escapedSpace) as text) end repeat set AppleScript's text item delimiters to "" set _pathString to _pathString as text set _pathString to (characters 1 thru -3 of _pathString) set _pathString to _pathString as text set end of _list to _pathString & " " end repeat set _list to _list as string set _list to text items 1 thru -2 of _list set _list to _list as text set _shArray to ("(" & _list & ")") as text set AppleScript's text item delimiters to original_delims -- {""} end tell return _shArrayend getEscapedPosixArray----- getHFSPaths( items ) ------- Description: Get list of items' HFS paths ( items )-- Input: Finder items-- Result: List of HFS pathson getHFSPaths(_items) if _items is not {} then try set _list to {} tell application "Finder" repeat with _item in _items set _path to (_item as text) set end of _list to _path end repeat end tell return _list on error _msg number _n beep display dialog ¬ "Couldn't get HFS path: " & {return} & {return} & ¬ _msg & {return} & "( error code " & _n & " )" with title ("Error") with icon (caution) end try end ifend getHFSPaths----- getHFSPathsQ( items ) ------- Description: Get list of items' HFS paths ( items )-- Input: Finder items-- Result: List of HFS pathson getHFSPathsQ(_items) if _items is not {} then try set _list to {} tell application "Finder" repeat with _item in _items set _path to (_item as text) set end of _list to (quoted form of _path) end repeat end tell return _list on error _msg number _n beep display dialog ¬ "Couldn't get HFS path: " & {return} & {return} & ¬ _msg & {return} & "( error code " & _n & " )" with title ("Error") with icon (caution) end try end ifend getHFSPathsQ----- getParentPosix( items ) ------- Description: Get the POSIX path of items' (item 1) parent folder-- Input: Finder items-- Result: Parent folder's POSIX pathon getParentPosix(_items) if _items is not {} then try tell application "Finder" set _container to container of item 1 of (_items) set _parent to POSIX path of (_container as string) end tell return _parent on error eMsg number eNum error "Can't getParent: " & eMsg number eNum end try end ifend getParentPosix----- getParentHFS( items ) -----on getParentHFS(_items) if _items is not {} then try tell application "Finder" set _container to container of item 1 of (_items) set _parent to (_container as string) end tell return _parent on error eMsg number eNum error "Can't getParent: " & eMsg number eNum end try end ifend getParentHFS----- getBasenameOfItem( item )------- Description: Get name of item ( item )-- Input: Finder item-- Result: basename of Finder itemon getBasenameOfItem(_item) if _item is not {} and _item is not "" then try tell application "Finder" set _fullname to name of _item if name extension of _item is not "" then set _here to -(offset of "." in ((reverse of text items of _fullname) as text)) - 1 set _basename to (text 1 thru _here of _fullname) set _basename to (_basename as string) else set _basename to _fullname end if end tell on error _msg number _n beep display dialog ¬ "Oops! Something went wrong: " & {return} & {return} & ¬ _msg & {return} & "( error code " & _n & " )" with title ("Error") with icon (caution) return end try end if return _basenameend getBasenameOfItem----------------------- getNameAndExt( item ) ------------------------- Description: Get name and file extension of a Finder item-- Input: Finder item-- Result: List...{name, extension}on getNameAndExt(_item) set _delimsOld to (AppleScript's text item delimiters) set _delimsNew to ("") set AppleScript's text item delimiters to (_delimsNew) tell application "Finder" set _name to (name of _item) set _nameLength to (length of _name) set _ext to (name extension of _item) set _extLength to (length of _ext) if _extLength is not 0 then set _basenameLength to (_nameLength - (_extLength + 1)) set _basename to ((text items 1 thru _basenameLength) of _name) set _basename to (_basename as string) else set _ext to ("") set _extLength to (0) set _basename to (_name as string) end if end tell ---------------------------------------------- set _extension to _ext set _basename to _basename set _nameList to {_basename, _extension} return _nameList ---------------------------------------------- set AppleScript's text item delimiters to _delimsOldend getNameAndExt----- getCommentOfItem( item )------- Description: Get Spotlight Comment of item ( item )-- Input: Finder item-- Result: Spotlight Comment of Finder itemon getCommentOfItem(_item) if _item is not {} and _item is not "" then try tell application "Finder" set _comment to comment of _item end tell on error _msg number _n beep display dialog ¬ "Oops! Something went wrong: " & {return} & {return} & ¬ _msg & {return} & "( error code " & _n & " )" with title ("Error") with icon (caution) return end try end if return _commentend getCommentOfItem----- getContainerOfItem( item )------- Description: Get Container Folder of item ( item )-- Input: Finder item-- Result: Container folder of Finder itemon getContainerOfItem(_item) if _item is not {} and _item is not "" then try tell application "Finder" set _container to container of _item end tell on error _msg number _n beep display dialog ¬ "Oops! Something went wrong: " & {return} & {return} & ¬ _msg & {return} & "( error code " & _n & " )" with title ("Error") with icon (caution) return end try end if return _containerend getContainerOfItem----- getPropertiesOfItem( item )------- Description: Get properties of item as list ( item )-- Input: Finder item-- Result: List of Finder item propertieson getPropertiesOfItem(_item) try tell application "Finder" set _properties to properties of _item end tell on error _msg number _n beep display dialog ¬ "Oops! Something went wrong: " & {return} & {return} & ¬ _msg & {return} & "( error code " & _n & " )" with title ("Error") with icon (caution) return end try return _propertiesend getPropertiesOfItem(*1. bounds2. comment3. container4. creation date5. creator type6. description7. desktop position8. disk9. displayed name10. everyones privileges11. extension hidden12. file type13. group14. group privileges15. icon16. index17. kind18. label index19. location20. locked21. modification date22. name23. name extension24. owner25. owner privileges26. extension hidden27. file type28. group29. group privileges30. icon31. index32. information window33. kind34. label index35. location36. locked37. modification date38. name39. name extension40. owner41. owner privileges42. physical size43. position44. product version45. properties46. size47. stationery48. URL49. version*)----- getParentAlias( alias ) ------- COPYRIGHT (c) 2008 ljr (http://applescript.bratis-lover.net)on getParentAlias(_alias) try tell application "Finder" set _parent to POSIX file ((POSIX path of _alias) & "/..") as alias return _parent end tell on error eMsg number eNum error "Can't getParent: " & eMsg number eNum end tryend getParentAlias----- appendBasenameToParentPOSIX( basenames, parentPOSIX ) ------- Description: Appends a list of basenames to parent folder's POSIX path-- Input: List of basenames or words-- Parameters: The POSIX path of a parent folder, or any folder for that matter-- Result: List of new POSIX paths using names from input---- Note: If input, "basenames" were prefiltered to exclude folders, you-- could use this function to get a list of anything not yet present-- as a folder, format it for shell (as an array variable) using the-- "changeListType" function, then pipe the result to a mkdir command -- thereby creating new folders (from names) in the parent directoryon appendBasenamesToParentPOSIX(_basenames, _parentPosix) if ¬ length of (_basenames as string) is not 0 and ¬ length of (_parentPosix as string) is not 0 then try set _newPaths to {} set _quotedForm to false --------------------------------------------------- set _newDelims to "" set _oldDelims to AppleScript's text item delimiters set AppleScript's text item delimiters to _newDelims --------------------------------------------------- set _parentString to _parentPosix as string set _item1 to _parentString's first text item set _itemN to _parentString's last text item ---------------------------------------------- if _item1 is "'" and _itemN is "'" then set _quotedForm to true set _parentString to text items 2 thru -2 of _parentString end if ---------------------------------------------- repeat with _bname in _basenames set _string to _bname as string set _itemA to _string's first text item set _itemZ to _string's last text item ---------------------------------------------- if _itemA is "'" and _itemZ is "'" then set _string to text items 2 thru -2 of _string end if ---------------------------------------------- set _itemPath to (_parentString & _string) as string ---------------------------------------------- if _quotedForm is true then set _itemPath to quoted form of _itemPath end if ---------------------------------------------- set end of _newPaths to _itemPath end repeat ---------------------------------------------- set AppleScript's text item delimiters to _oldDelims ---------------------------------------------- on error _msg number _n beep display dialog ¬ "Oops! Something went wrong: " & {return} & {return} & ¬ _msg & {return} & "( error code " & _n & " )" with title ("Error") with icon (caution) return end try end if ---------------------------------------------- return _newPaths ----------------------------------------------end appendBasenamesToParentPOSIX---------------------------------------------- EOF ----------------------------------------------