{"id":1211,"date":"2017-03-09T12:35:43","date_gmt":"2017-03-09T19:35:43","guid":{"rendered":"http:\/\/markalldritt.com\/?p=1211"},"modified":"2017-03-09T12:35:43","modified_gmt":"2017-03-09T19:35:43","slug":"markslib","status":"publish","type":"post","link":"https:\/\/markalldritt.com\/?p=1211","title":{"rendered":"MarksLib"},"content":{"rendered":"<p>My MarksLib AppleScript library, which is featured in some of my Script Debugger tutorial videos, is now part of my <a href=\"https:\/\/github.com\/alldritt\/AppleScriptLibraries\">AppleScript Libraries repository on GitHub<\/a>.<\/p>\n\n<p>MarksLib is a collection of handlers for everyday operations that I find myself using in almost every script I write.  There are tools for reading and writing text files, string substitution, converting between strings and arrays and more.<\/p>\n\n<h2>Installation<\/h2>\n\n<p>Enter the following command in the <code>Terminal<\/code> application to install the latest version of MarksLib on your machine:<\/p>\n\n<pre><code>curl https:\/\/raw.githubusercontent.com\/alldritt\/AppleScriptLibraries\/master\/MarksLib.applescript | osacompile -o ~\/Library\/Script\\ Libraries\/MarksLib.scpt\n<\/code><\/pre>\n\n<h2>Usage<\/h2>\n\n<p>MarksLib provides the following handlers:<\/p>\n\n<h3>readFromFile<\/h3>\n\n<p>The <code>readFromFile(theFile)<\/code> handler reads the contents of a text file.  The <code>theFile<\/code> parameter can take many forms:<\/p>\n\n<ul>\n<li>full HFS path (e.g. <code>readFromFile(\"Macintosh HD:Users:Mark:Desktop:file.txt\")<\/code>)<\/li>\n<li>full POSIX path (e.g. <code>readFromFile(\"\/Users\/Mark\/Desktop\/file.txt\")<\/code>)<\/li>\n<li>relative POSIX path (e.g.) (<code>readFromFile(\"~\/Desktop\/file.txt\")<\/code>)<\/li>\n<li>alias (e.g. <code>readFromFile(alias \"Macintosh HD:Users:Mark:Desktop:file.txt\")<\/code>)<\/li>\n<li>file reference (e.g. <code>readFromFile(file \"Macintosh HD:Users:Mark:Desktop:file.txt\")<\/code>)<\/li>\n<\/ul>\n\n<h3>writeToFile<\/h3>\n\n<p>The <code>writeToFile(theFile, theData)<\/code> handler writes the contents of a variable to a text file.  The <code>theFile<\/code> parameter can take many forms:<\/p>\n\n<ul>\n<li>full HFS path (e.g. <code>readFromFile(\"Macintosh HD:Users:Mark:Desktop:file.txt\")<\/code>)<\/li>\n<li>full POSIX path (e.g. <code>readFromFile(\"\/Users\/Mark\/Desktop\/file.txt\")<\/code>)<\/li>\n<li>relative POSIX path (e.g.) (<code>readFromFile(\"~\/Desktop\/file.txt\")<\/code>)<\/li>\n<li>alias (e.g. <code>readFromFile(alias \"Macintosh HD:Users:Mark:Desktop:file.txt\")<\/code>)<\/li>\n<li>file reference (e.g. <code>readFromFile(file \"Macintosh HD:Users:Mark:Desktop:file.txt\")<\/code>)<\/li>\n<\/ul>\n\n<p>The <code>theData<\/code> parameter should be a string.<\/p>\n\n<h3>replaceText<\/h3>\n\n<p>The <code>replaceText(theString, fString, rString)<\/code> handler replaces all occurrences of <code>fString<\/code> in <code>theString<\/code> with <code>rString<\/code>.<\/p>\n\n<pre><code>use AppleScript version \"2.4\" -- Yosemite (10.10) or later\nuse MarksLib : script \"MarksLib\" version \"1.0\"\n\nMarksLib's replaceText(\"the quick brown fox jumped over the lazy dog\", \"the\", \"xxx\")\n--&gt;\"xxx quick brown fox jumped over xxx lazy dog\"\n<\/code><\/pre>\n\n<h3>trim<\/h3>\n\n<p>The <code>trim(theString)<\/code> handler trims leading and trailing whitespace from a string.  The functional also works on arrays of strings, returning an array of trimmed strings.<\/p>\n\n<pre><code>use AppleScript version \"2.4\" -- Yosemite (10.10) or later\nuse MarksLib : script \"MarksLib\" version \"1.0\"\n\nMarksLib's trim(\"  hello world  \" &amp; return)\n--&gt;\"hello world\"\nMarksLib's trim({\" abc \", \"1234   \", \"   \"})\n--&gt;{\"abc\", \"1234\", \"\"}\n<\/code><\/pre>\n\n<h3>split<\/h3>\n\n<p>The <code>split(theString, theSeparator)<\/code> handler splits a string into an array of strings.  <\/p>\n\n<pre><code>use AppleScript version \"2.4\" -- Yosemite (10.10) or later\nuse MarksLib : script \"MarksLib\" version \"1.0\"\n\nMarksLib's split(\"1, 2, 3, 4\", \",\")\n--&gt;{\"1\", \" 2\", \" 3\", \" 4\"}\nMarksLib's trim(MarksLib's split(\"1, 2, 3, 4\", \",\"))\n--&gt;{\"1\", \"2\", \"3\", \"4\"}\n<\/code><\/pre>\n\n<h3>join<\/h3>\n\n<p>The <code>|join|(theString, theSeparator)<\/code> handler joins an array of strings together into a single string.<\/p>\n\n<pre><code>use AppleScript version \"2.4\" -- Yosemite (10.10) or later\nuse MarksLib : script \"MarksLib\" version \"1.0\"\n\nMarksLib's join({\"hello\", \"world\"}, \" \")\n--&gt;{\"hello world\"}\nMarksLib's join(MarksLib's trim(MarksLib's split(\"1, 2, 3, 4\", \",\")), \"-\")\n--&gt;{\"1-2-3-4\"} \n<\/code><\/pre>\n\n<h3>formatForJSON<\/h3>\n\n<p>The <code>formatForJSON(theValue)<\/code> handler formats a string so that it is suitable for use as a value in a JSON structure.<\/p>\n\n<pre><code>use AppleScript version \"2.4\" -- Yosemite (10.10) or later\nuse MarksLib : script \"MarksLib\" version \"1.0\"\n\nMarksLib's formatForJSON(\"My name is \\\"Mark\\\"\")\n--&gt;\"My name is \\\"Mark\\\"\"\n<\/code><\/pre>\n\n<h3>formatForCSV<\/h3>\n\n<p>The <code>formatCSVString(theValue)<\/code> handler formats a string so that is suitable for use as a value in a CSV file.<\/p>\n\n<pre><code>use AppleScript version \"2.4\" -- Yosemite (10.10) or later\nuse MarksLib : script \"MarksLib\" version \"1.0\"\n\nMarksLib's formatCSVString(\"Nothing special\")\n--&gt;\"Nothing special\"\nMarksLib's formatCSVString(\"My name is \\\"Mark\\\"\")\n--&gt;\"My name is \"\"Mark\"\"\"\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>MarksLib is a collection of handlers for everyday operations that I find myself using in almost every script I write.  There are tools for reading and writing text files, string substitution, converting between strings and arrays and more.<\/p><div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/markalldritt.com\/?p=1211\">Continue Reading<span class=\"screen-reader-text\">MarksLib<\/span><\/a><\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[6],"tags":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/s7AQk-markslib","_links":{"self":[{"href":"https:\/\/markalldritt.com\/index.php?rest_route=\/wp\/v2\/posts\/1211"}],"collection":[{"href":"https:\/\/markalldritt.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/markalldritt.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/markalldritt.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/markalldritt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1211"}],"version-history":[{"count":5,"href":"https:\/\/markalldritt.com\/index.php?rest_route=\/wp\/v2\/posts\/1211\/revisions"}],"predecessor-version":[{"id":1216,"href":"https:\/\/markalldritt.com\/index.php?rest_route=\/wp\/v2\/posts\/1211\/revisions\/1216"}],"wp:attachment":[{"href":"https:\/\/markalldritt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/markalldritt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/markalldritt.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}