{"id":280,"date":"2006-08-26T16:14:52","date_gmt":"2006-08-26T23:14:52","guid":{"rendered":"http:\/\/strawhousepig.net\/wp\/?p=280"},"modified":"2006-08-26T16:14:52","modified_gmt":"2006-08-26T23:14:52","slug":"apache-the-www-user-applescript-and-me","status":"publish","type":"post","link":"https:\/\/strawhousepig.net\/wordpress\/2006\/08\/26\/apache-the-www-user-applescript-and-me\/","title":{"rendered":"Apache, the www user, AppleScript, and me"},"content":{"rendered":"<p>The answer my friend, pops up to tell you when someone drops a file in your Drop Box!<br \/>\n<!--more--><br \/>\nWell, us, really.<\/p>\n<p>Since I never felt comfortable telling people to run Apache as their user (which is giving a web server read-write privileges to everything you have those privileges for!) I&#8217;ve been searching for another way to poll iTunes for info and send commands to it via a web interface, because the www user (Apache and PHP) can&#8217;t do it directly through AppleScript.<\/p>\n<p>In searching for a solution, I found <del datetime=\"2006-08-27T16:07:52+00:00\">Brian D. Foy&#8217;s<\/del> <ins datetime=\"2006-08-27T16:07:52+00:00\">Matthew Russell&#8217;s<\/ins> excellent article <a href=\"http:\/\/www.macdevcenter.com\/pub\/a\/mac\/2005\/03\/01\/itunes_remote.html\">MacDevCenter.com &#8212; Build an iTunes Remote Control<\/a> on using a shell daemon to read a temp file and pass what it finds to osascript. I tried out his scripts, but (under 10.4, at least) the daemon script&#8217;s process name is bash, making the companion script used to kill it worthless. So I says to myself, &#8220;Self, looks like you&#8217;re gonna be learning some Perl.&#8221;<\/p>\n<p>Wrong. I&#8217;m lazy, I admit it. But I have a very good reason to make this project easier: <a href=\"http:\/\/developer.apple.com\/documentation\/AppleScript\/Conceptual\/AppleScriptX\/Concepts\/as_related_apps.html#\/\/apple_ref\/doc\/uid\/TP40001570-1148133\">Folder Actions<\/a>. A daemon that already runs and performs any AppleScript you can write based on the conditions you give it.<\/p>\n<p>What now? I&#8217;ll tell you what now.<\/p>\n<ul>\n<li>Scrap the PHP &#8220;exec(osascript)&#8221; from the current package.<\/li>\n<li>Write &#8220;tempnam()&#8221; routines for the control input that write commands to a file. The file name will include the user name (taken from iTunesDSr &#8220;pwd&#8221;) so the next step can use it properly.<\/li>\n<li>Write a Folder Actions script that runs when a file pops up in the tmp directory. If it&#8217;s an appropriately named file, it will read it and pass the commands to iTunes.<\/li>\n<li>If possible, use this same Folder Action to write a file containing iTunes current info, another file for all playlist names, perhaps another with current playlist track names. If not, write a companion, stay-open AppleScript app that can.<\/li>\n<\/ul>\n<p>The hard part, perhaps even a sticking point, will be timing these scripts so they fire when needed, but not after the next step in another script has passed. In first tests, the PHP <strong>tempnam()<\/strong> routine must <strong>sleep()<\/strong> for 2 seconds to give Folder Actions a chance to see the file.<\/p>\n<p>Given that, this method will be more sluggish than what I wanted it to be. But if it keeps some a-hole, or some worm from reading your user files (or worse), it&#8217;s well worth the inconvenience.<\/p>\n<p>Thanks to brian d foy for correcting my mistake on citing his Perl article (<a href=\"http:\/\/www.macdevcenter.com\/pub\/a\/mac\/2002\/11\/22\/itunes_perl.html\">MacDevCenter.com &#8212; Controlling iTunes with Perl<\/a>) and for pointing out the Perl extension Mac::AppleScript (<a href=\"http:\/\/search.cpan.org\/~dsugal\/Mac-AppleScript-0.04\/AppleScript.pm\">Mac::AppleScript &#8211; Perl extension to execute applescript commands on OS X &#8211; search.cpan.org<\/a>). If this project interests you I suggest you check it out.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The answer my friend, pops up to tell you when someone drops a file in your Drop Box!<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,7],"tags":[10],"class_list":["post-280","post","type-post","status-publish","format-standard","hentry","category-code","category-web","tag-applescript-2"],"_links":{"self":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/posts\/280","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/comments?post=280"}],"version-history":[{"count":0,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/posts\/280\/revisions"}],"wp:attachment":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/media?parent=280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/categories?post=280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/tags?post=280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}