SHARE
TWEET

Some Folder and file handlers

applehelpwriter Jul 26th, 2016 (edited) 146 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ###########################################################
  2. # ABOUT
  3. ###########################################################
  4. (*
  5.  
  6.  Phil Stokes
  7.  applehelpwriter.com
  8.  sqwarq.com 2016
  9.  
  10. *)
  11. ###########################################################
  12. # DESCRIPTION
  13. ###########################################################
  14. (*
  15.  
  16. 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).
  17.  
  18.  
  19.  
  20. *)
  21. ###########################################################
  22. # USAGE
  23. ###########################################################
  24. (*
  25.  
  26. In all three cases, you give the handler a path string in POSIX form, e.g, ~/Desktop or (for example 3), ~/Desktop/sometext.txt.
  27.  
  28. 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.
  29.  
  30. 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.
  31.  
  32. 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.
  33. Example 3 needs OS X 10.10 or higher.
  34.  
  35. Hope these come in as handy for you folks as they have for me!
  36.  
  37.  
  38. *)
  39. ###########################################################
  40. # IMPORT STATEMENTS
  41. ###########################################################
  42.  
  43. use AppleScript version "2.4" -- Yosemite (10.10) or later
  44. use scripting additions
  45. use framework "Foundation"
  46.  
  47.  
  48.  
  49. ###########################################################
  50. # VARIABLES
  51. ###########################################################
  52.  
  53.  
  54.  
  55.  
  56. ###########################################################
  57. # HANDLERS
  58. ###########################################################
  59.  
  60. # 1 item names only, returns list of Posix strings
  61. on getFolderItemNames(aPath)
  62.     -- spaces in aPath need to be double-escaped
  63.     set f to paragraphs of (do shell script "cd " & aPath & "; ls")
  64.     return f as list
  65. end getFolderItemNames
  66. # example
  67. # set d to getFolderItemNames("~/Desktop")
  68.  
  69.  
  70. # 2 get items with all their properties, returns a record
  71. on getFolderItemsWithProperties(aPath)
  72.     tell application "System Events"
  73.         if text 1 of aPath is "~" then
  74.             set hf to POSIX path of home folder
  75.             set aPath to hf & (text 2 thru -1 of aPath)
  76.         end if
  77.         set f to aPath
  78.         set aPath to (POSIX file f) as alias
  79.         set f to properties of (every item in aPath) as list
  80.         return f
  81.     end tell
  82. end getFolderItemsWithProperties
  83. # example
  84. # set theProps to getFolderItemNames("/var/tmp")
  85. # set sz to theProps's item 3's size
  86.  
  87.  
  88. # 3. get the contents of a file by specifying its path, returns a string
  89. on getFileContents(aFilePath)
  90.     -- requires OS X 10.10 or higher
  91.     tell application "System Events"
  92.         if text 1 of aFilePath is "~" then
  93.             set hf to POSIX path of home folder
  94.             set aFilePath to hf & (text 2 thru -1 of aFilePath)
  95.         end if
  96.     end tell
  97.     set aURL to current application's NSURL's fileURLWithPath:aFilePath
  98.     set aString to current application's NSString's stringWithContentsOfURL:aURL
  99.    
  100.     return aString as text
  101. end getFileContents
  102.  
  103. # # example:
  104. # set c to getFileContents("/var/tmp/spindump_sysdiagnose_2016.06.18_11-32-57+0700.txt")
  105. # set p to paragraphs of c
  106. # set resultList to {}
  107. # repeat with i from 1 to count of items in p
  108. #   set this_item to item i of p
  109. #   if this_item contains "CALLING_OUT" then
  110. #       set end of resultList to this_item
  111. #   end if
  112. # end repeat
  113. # resultList
  114.  
  115.  
  116. ###########################################################
  117. # COMMANDS
  118. ###########################################################
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130. ###########################################################
  131. #EOF
RAW Paste Data
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top