Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ###########################################################
- # ABOUT
- ###########################################################
- (*
- Phil Stokes
- applehelpwriter.com
- sqwarq.com 2016
- *)
- ###########################################################
- # DESCRIPTION
- ###########################################################
- (*
- Here's a few of the handlers I use for getting the contents of folders (examples 1 & 2), or for getting the text of a file (example 3).
- *)
- ###########################################################
- # USAGE
- ###########################################################
- (*
- In all three cases, you give the handler a path string in POSIX form, e.g, ~/Desktop or (for example 3), ~/Desktop/sometext.txt.
- In example 1, spaces must be double-escaped, (\\ for one space); what you get back is a list of the item names in the folder. It doesn't include hidden or invisible files.
- In example 2, spaced do not need to be esacped; what you get back is a record of all the items and their properties. This can be an immensely useful and powerful handler.
- In example 3, spaces do not need to be escaped; what you get back is a text variable whose value is the complete text of the file.
- Example 3 needs OS X 10.10 or higher.
- Hope these come in as handy for you folks as they have for me!
- *)
- ###########################################################
- # IMPORT STATEMENTS
- ###########################################################
- use AppleScript version "2.4" -- Yosemite (10.10) or later
- use scripting additions
- use framework "Foundation"
- ###########################################################
- # VARIABLES
- ###########################################################
- ###########################################################
- # HANDLERS
- ###########################################################
- # 1 item names only, returns list of Posix strings
- on getFolderItemNames(aPath)
- -- spaces in aPath need to be double-escaped
- set f to paragraphs of (do shell script "cd " & aPath & "; ls")
- return f as list
- end getFolderItemNames
- # example
- # set d to getFolderItemNames("~/Desktop")
- # 2 get items with all their properties, returns a record
- on getFolderItemsWithProperties(aPath)
- tell application "System Events"
- if text 1 of aPath is "~" then
- set hf to POSIX path of home folder
- set aPath to hf & (text 2 thru -1 of aPath)
- end if
- set f to aPath
- set aPath to (POSIX file f) as alias
- set f to properties of (every item in aPath) as list
- return f
- end tell
- end getFolderItemsWithProperties
- # example
- # set theProps to getFolderItemNames("/var/tmp")
- # set sz to theProps's item 3's size
- # 3. get the contents of a file by specifying its path, returns a string
- on getFileContents(aFilePath)
- -- requires OS X 10.10 or higher
- tell application "System Events"
- if text 1 of aFilePath is "~" then
- set hf to POSIX path of home folder
- set aFilePath to hf & (text 2 thru -1 of aFilePath)
- end if
- end tell
- set aURL to current application's NSURL's fileURLWithPath:aFilePath
- set aString to current application's NSString's stringWithContentsOfURL:aURL
- return aString as text
- end getFileContents
- # # example:
- # set c to getFileContents("/var/tmp/spindump_sysdiagnose_2016.06.18_11-32-57+0700.txt")
- # set p to paragraphs of c
- # set resultList to {}
- # repeat with i from 1 to count of items in p
- # set this_item to item i of p
- # if this_item contains "CALLING_OUT" then
- # set end of resultList to this_item
- # end if
- # end repeat
- # resultList
- ###########################################################
- # COMMANDS
- ###########################################################
- ###########################################################
- #EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement