{"id":743,"date":"2016-08-21T18:55:11","date_gmt":"2016-08-21T23:55:11","guid":{"rendered":"http:\/\/www.lorenzcom.com\/?p=743"},"modified":"2016-08-22T10:16:20","modified_gmt":"2016-08-22T15:16:20","slug":"filelib-file-routines","status":"publish","type":"post","link":"https:\/\/lorenzcom.com\/?p=743","title":{"rendered":"FileLib &#8211; File Routines Library"},"content":{"rendered":"<h3>FileLib<\/h3>\n<p>FileLib is a free AppleScript library of essential folder and file routines for use in AppleScript scripts and applications.<\/p>\n<h4>FileLib Handlers<\/h4>\n<ul>\n<li><strong>WriteToFile ( _data , _filename , _append )<\/strong> &#8211; Write data to filename and either overwrites the filename or append the data to it.<\/li>\n<li><strong>ReadFromFile ( _filename , _delimiter )<\/strong> &#8211; Read from the filename up to the delimiter character passed. Reads in whole file if delimiter is null.<\/li>\n<li><strong>UniqueFileName ( _path , _name , _extension , _delimiters )<\/strong> &#8211; Returns a unique file name. A sequential number, surrounding by the characters in delimiters list, is appended to the end the name. Delimiters default to { &#8220;_&#8221; , &#8220;&#8221; } if null. E.g., &#8220;Untitled_1.pdf&#8221;, &#8220;Untitled_2.pdf&#8221;, &#8220;Untitled_3.pdf&#8221;, etc.<\/li>\n<li><strong>DeleteFile ( _path , _filename )<\/strong> &#8211; Deletes the file.<\/li>\n<li><strong>TestExists ( _path , _type )<\/strong> &#8211; Use &#8220;test&#8221; shell script command to test for either a directory (type = &#8220;d&#8221;) or file (type = &#8220;f&#8221;). Returns true if it exists and false if it doesn&#8217;t.<\/li>\n<li><strong>FolderExists ( _path )<\/strong> &#8211; Use Finder to test whether a folder or directory exists. Returns true if it exists and false if it doesn&#8217;t.<\/li>\n<li><strong>FileExists ( _path )<\/strong> &#8211; Use Finder to test whether a file exists. Returns true if it exists and false if it doesn&#8217;t.<\/li>\n<\/ul>\n<h4>Free AppleScript Library<\/h4>\n<p><strong>FileLib Library Code:<\/strong> Copy the selected code (click on Select All to highlight all the code) to the clipboard and paste it into a newly created Custom Function. <a href=\"\/docs\/AppleScript\/FileLib.scpt\">Download code as a text file from here.<\/a><\/p>\n<form>\n<textarea id=<?php echo '\"te_'.get_the_ID().'\"';?> style=&#8221;width:100%; height:180px; font-family:Arial,Helvetica; font-size:.90em; line-height:normal;&#8221;><!--raw-->on WriteToFile(_data, _fileName, _appendFile)<br \/>\n\t&#8211;Format: my WriteToFile(_data, _fileName, _appendFile)<br \/>\n\tset _fileName to _fileName as text<br \/>\n\ttry<br \/>\n\t\tset _fileRef to open for access file _fileName with write permission<br \/>\n\t\tif not _appendFile then set eof of _fileRef to 0 &#8211;False = overwrite, True = append.<br \/>\n\t\twrite _data to _fileRef starting at eof<br \/>\n\t\tclose access _fileRef<br \/>\n\t\treturn true &#8211;Success.<br \/>\n\ton error<br \/>\n\t\tclose access _fileRef<br \/>\n\t\tdisplay alert &#8220;WriteToFile error: &#8221; &#038; _eNum &#038; &#8221; &#8221; &#038; _eMess buttons {&#8220;Continue&#8221;}<br \/>\n\t\treturn false &#8211;Failure.<br \/>\n\tend try<br \/>\n\t&#8211;if (_fileRef is not missing value) then close access _fileRef &#8211;Not needed since the access is closed in the try.<br \/>\nend WriteToFile<\/p>\n<p>on ReadFromFile(_fileName, _delimiter)<br \/>\n\t&#8211;Returns a a list containing the file contents separated by the delimiter.<br \/>\n\t&#8211;Null delimiter returns the entire un-separated file contents.<br \/>\n\tset _fileName to _fileName as text<br \/>\n\tset _delimiter to _delimiter as text<br \/>\n\ttry<br \/>\n\t\tset _fileRef to (open for access _fileName)<br \/>\n\t\tset _fileContents to read _fileRef using delimiter _delimiter<br \/>\n\t\tclose access _fileRef<br \/>\n\t\treturn _fileContents<br \/>\n\ton error _eMess number _eNum<br \/>\n\t\tclose access _fileRef<br \/>\n\t\tdisplay alert &#8220;ReadFromFile error: &#8221; &#038; _eNum &#038; &#8221; &#8221; &#038; _eMess buttons {&#8220;Continue&#8221;}<br \/>\n\t\treturn false &#8211;Failure.<br \/>\n\tend try<br \/>\n\t&#8211;if (_fileRef is not missing value) then close access _fileRef &#8211;Not needed since the access is closed in the try.<br \/>\nend ReadFromFile<\/p>\n<p>on UniqueFilename(_path, _name, _extension, _delimiters)<br \/>\n\t&#8211;Returns a file or folder _name unique to the contents of the passed _path.<br \/>\n\t&#8211;Format UniqueFilename(text_path, text_name, text_extension, _delimiters)<br \/>\n\t&#8211;Appends a number enclosed by the left and right delimiter charcters to obtain a unique name.<br \/>\n\t&#8211;Do not return a file name with extrension unless _name is sent with an extension that equals _extension.<br \/>\n\t&#8211;the _extension indicates the extension, if present in _name, to be removed for unique naming. Reattach only if in _name.<br \/>\n\tset _path to _path as text<br \/>\n\tset _name to _name as text<br \/>\n\tset _extension to _extension as text<br \/>\n\tif _delimiters is &#8220;&#8221; or (count of _delimiters) < 2 then set _delimiters to {\"_\", \"\"} --Default delimiters.\n\tset _extensionIsDirectory to ((offset of _extension in \":\/\") > 0) as boolean<br \/>\n\tset _maxLength to 255 &#8211;Max filename length. 1024 for directory names.<br \/>\n\tif _extensionIsDirectory then set _maxLength to 1024<br \/>\n\tset _testExtension to &#8220;&#8221; &#8211;_extension will be &#8220;&#8221;, &#8220;:&#8221;, &#8220;\/&#8221;, or character extension without the period.<br \/>\n\tif _extension is not equal to &#8220;&#8221; and not _extensionIsDirectory then set _testExtension to &#8220;.&#8221; &#038; _extension &#8211;Create &#8220;.x&#8230;x&#8221; character extension, else it is empty or a folder separator.<br \/>\n\tset _extensionOffset to offset of _testExtension in _name &#8211;Check if in _name.<br \/>\n\tset _extensionIsInName to (_extensionOffset > 0) as boolean &#8211;If _extension is in _name, reattach it to unique name at end.<br \/>\n\tset _trimmedName to _name<br \/>\n\tif _extensionOffset is equal to (((length of _name) &#8211; (length of _testExtension)) + 1) then set _trimmedName to text 1 thru (_extensionOffset &#8211; 1) of _name &#8211;Remove the _extension if at the end of _name.<br \/>\n\tset _testType to &#8220;-f &#8221; &#8211;&#8220;file&#8221;<br \/>\n\tif _extensionIsDirectory then set _testType to &#8220;-d &#8221; &#8211;&#8220;directory&#8221;<br \/>\n\tset _testName to _trimmedName &#8211;Default to the sent name without extension.<br \/>\n\t&#8211;tell application &#8220;Finder&#8221;<br \/>\n\ttell current application<br \/>\n\t\ttry &#8211;Without on error trap.<br \/>\n\t\t\tif (do shell script &#8220;test &#8221; &#038; _testType &#038; quoted form of POSIX path of (_path &#038; _testName &#038; _testExtension)) = &#8220;&#8221; then &#8211;If this name is the same.<br \/>\n\t\t\t\tset _nameIncrement to 1 &#8211;Add increment to get unique name.<br \/>\n\t\t\t\trepeat<br \/>\n\t\t\t\t\tset _testName to (_trimmedName &#038; item 1 of _delimiters &#038; (_nameIncrement as text) &#038; item 2 of _delimiters)<br \/>\n\t\t\t\t\tif not ((do shell script &#8220;test &#8221; &#038; _testType &#038; quoted form of POSIX path of (_path &#038; _testName &#038; _testExtension)) = &#8220;&#8221;) then<br \/>\n\t\t\t\t\t\texit repeat<br \/>\n\t\t\t\t\telse<br \/>\n\t\t\t\t\t\tset the _nameIncrement to _nameIncrement + 1 &#8211;Increment the counter.<br \/>\n\t\t\t\t\t\tif length of (_path &#038; _testName &#038; _testExtension &#038; (_nameIncrement as text) &#038; (_delimiters as text)) > _maxLength then<br \/>\n\t\t\t\t\t\t\tif length of _trimmedName > 1 then<br \/>\n\t\t\t\t\t\t\t\tset _trimmedName to text 1 thru ((length of _trimmedName) &#8211; 1) of _trimmedName &#8211;Reduce trimmed name size.<br \/>\n\t\t\t\t\t\t\t\tif _trimmedName = &#8220;&#8221; then return &#8220;&#8221; &#8211;Return empty string if trimmed to null.<br \/>\n\t\t\t\t\t\t\t\tset _testName to _trimmedName &#8211;Set _testName for return out of repeat.<br \/>\n\t\t\t\t\t\t\t\tif not ((do shell script &#8220;test &#8221; &#038; _testType &#038; quoted form of POSIX path of (_path &#038; _testName &#038; _testExtension)) = &#8220;&#8221;) then exit repeat<br \/>\n\t\t\t\t\t\t\t\tset _nameIncrement to 1 &#8211;Restart the increment.<br \/>\n\t\t\t\t\t\t\telse<br \/>\n\t\t\t\t\t\t\t\treturn &#8220;&#8221; &#8211;Return null too short to trim.<br \/>\n\t\t\t\t\t\t\tend if<br \/>\n\t\t\t\t\t\tend if<br \/>\n\t\t\t\t\tend if<br \/>\n\t\t\t\tend repeat<br \/>\n\t\t\tend if<br \/>\n\t\tend try<br \/>\n\tend tell<br \/>\n\tif _extensionIsInName then set _testName to _testName &#038; _testExtension &#8211;Reattach extension if present in _name.<br \/>\n\treturn _testName &#8211;File or folder name unique to _path contents.<br \/>\nend UniqueFilename<\/p>\n<p>on DeleteFile(_path, _fileName) &#8211;Deletes a file only if it exists.<br \/>\n\ttell application &#8220;Finder&#8221;<br \/>\n\t\trepeat until not (exists document file _fileName of _path)<br \/>\n\t\t\ttry<br \/>\n\t\t\t\tdelete document file _fileName of _path<br \/>\n\t\t\ton error number _eNum<br \/>\n\t\t\t\tif _eNum = -128 then exit repeat &#8211;Catch cancel.<br \/>\n\t\t\tend try<br \/>\n\t\tend repeat<br \/>\n\tend tell<br \/>\nend DeleteFile<\/p>\n<p>on TestExists(_path, _type)<br \/>\n\t&#8211;_type says _path is a directory or file (d or f).<br \/>\n\tset _script to &#8220;test -&#8221; &#038; (character 1 of _type) &#038; &#8221; &#8221; &#038; quoted form of POSIX path of _path<br \/>\n\ttry<br \/>\n\t\treturn ((do shell script _script) = &#8220;&#8221;) as boolean<br \/>\n\t\treturn true<br \/>\n\ton error _eMess number _eNum<br \/>\n\t\treturn false<br \/>\n\t\t&#8211;if _eNum = 1 then return false &#8211;1 = False, 0  = True<br \/>\n\t\t&#8211;display dialog _eNum &#038; &#8221; &#8221; &#038; _eMess with title &#8220;TestExists Error&#8221;<br \/>\n\tend try<br \/>\nend TestExists<\/p>\n<p>on FolderExists(_path)<br \/>\n\t&#8211;Avoid tell.. end tell blocks inother code.<br \/>\n\ttell application &#8220;Finder&#8221; to return folder (_path) exists &#8211;True if exists, else False.<br \/>\nend FolderExists<\/p>\n<p>on FileExists(_path)<br \/>\n\t&#8211;Avoid tell.. end tell blocks in other code.<br \/>\n\ttell application &#8220;Finder&#8221; to return file (_path) exists &#8211;True or False, if exists.<br \/>\nend FileExists<br \/>\n<!--\/raw--><\/textarea><br \/>\n<input onclick=<?php echo '\"this.form.te_'.get_the_ID().'.focus();this.form.te_'.get_the_ID().'.select();\"';?> type=&#8221;button&#8221; value=&#8221;Select All&#8221; \/> <input type=\"reset\" value=\"Reset\" \/><\/form>\n","protected":false},"excerpt":{"rendered":"<p>FileLib FileLib is a free AppleScript library of essential folder and file routines for use in AppleScript scripts and applications. FileLib Handlers WriteToFile ( _data , _filename , _append ) &#8211; Write data to filename and either overwrites the filename <span class=\"excerpt-dots\">&hellip;<\/span> <a class=\"more-link\" href=\"https:\/\/lorenzcom.com\/?p=743\"><span class=\"more-msg\">Continue reading &rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[12,10,42],"tags":[],"_links":{"self":[{"href":"https:\/\/lorenzcom.com\/index.php?rest_route=\/wp\/v2\/posts\/743"}],"collection":[{"href":"https:\/\/lorenzcom.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lorenzcom.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lorenzcom.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lorenzcom.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=743"}],"version-history":[{"count":8,"href":"https:\/\/lorenzcom.com\/index.php?rest_route=\/wp\/v2\/posts\/743\/revisions"}],"predecessor-version":[{"id":753,"href":"https:\/\/lorenzcom.com\/index.php?rest_route=\/wp\/v2\/posts\/743\/revisions\/753"}],"wp:attachment":[{"href":"https:\/\/lorenzcom.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lorenzcom.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=743"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lorenzcom.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}