 |
 |
Can perl send and recieve Apple Events?
|
 |
|
 |
|
Addicted to MacNN
Join Date: Jan 2000
Location: Stoneham, MA, USA
Status:
Offline
|
|
I have approximately 11 GB of music on my computer. All the music that I have is on my computer. No CD's are unripped. I ALWAYS listen to music while I'm at my computer. My G4 is plugged right into the aux on my stereo. I never listen to FM in my house, always MP3. I've been wanting to start streaming audio so visitors to my web site can listen to what i'm listening to when I'm listening to it. I'm on cable though, so I don't have nearly enough speed to stream, especially since my page is heavy on the graphics, and since I do have that one-download-spot hotline server running all the time too. It occurred to me today, since I got my perl counter working perfectly, and since I had already gotten UBB running on my site.... why not make a perl script as a SSI that would display the current track I'm listening to on my home page, so visitors could at least know what I was listening to, if not hear it themselves. I think its the damn coolest thing, and none of you will convince me other wise ;-)
So the question is, can perl send and receive Apple Events? AppleScript CGI's are out of the question due to the complexity and intrusive way they have to run as CGI's.
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>tell application <font color = red>"iTunes"</font>
{artist of current track, name of current track, album of current track, year of current track as string}
end tell</font>[/code]
The AppleScript above is exactly what I need. If there was a way I could have perl send those events, and grab the results, I could wrap that into some sort of text or graphic. It would be awesome. Keep in mind (PAUL) that I am still relatively (VERY) new to perl, and a page full of perl isn't going to be terribly helpful. Piece by piece one at a time please.
[ 02-18-2002: Message edited by: l008com ]
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Jul 2001
Location: Mount Vernon, WA
Status:
Offline
|
|
|
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Oct 1999
Location: San Jose, Ca
Status:
Offline
|
|
I think this should make you happy, it is a perl plugin that allows for most of the MacPerl functionality, most of which is apple event support.
http://news2.ils.uec.ac.jp/~herr/
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Jan 2000
Location: Stoneham, MA, USA
Status:
Offline
|
|
What is MacPerl? Also installing a module for apache sounds very complicated.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Oct 1999
Location: San Jose, Ca
Status:
Offline
|
|
The file I referenced is simply a perl module, there is no apache module to install. I am not sure where you got that idea...
As to MacPerl, it is a version of perl that runs on MacOS 8-9, and includes a few special extensions that allow for better integration into that OS. Most importanly for this conversation it allows for AppleScripts to be run from inside a perl program.
OSXMacPerl simply wraps a call to the command line utility 'osascript' and makes it more MacPerl-like.
PS.. installing a pre-compiles apache module is simple...
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Jan 2000
Location: Stoneham, MA, USA
Status:
Offline
|
|
So then how would I go about using that thing you linked me to?
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Feb 2002
Location: San Francisco
Status:
Offline
|
|
alternative solution.. use the itunes snappertag in snapperhead
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Oct 1999
Location: San Jose, Ca
Status:
Offline
|
|
You use OSXMacPerl just like you would any other perl module (hint: there are directions inside the file...). If that means nothing to you, then you need a lot more help than I am going to provide (ie: you need someone to write the program for you).
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Jan 2000
Location: Stoneham, MA, USA
Status:
Offline
|
|
Well does anyone other than larkost actually wanna show me how to use this thing?
|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Oct 2001
Location: Oxford, OH
Status:
Offline
|
|
cp OSXMacPerl-0.1.pm /Library/perl/darwin/MacPerl.pm
and in your Perl script, use MacPerl;.
Now how would I get the result of this iTunes script snippet into a variable or print it out...?
[ 03-01-2002: Message edited by: zekemon6 ]
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Jan 2000
Location: Stoneham, MA, USA
Status:
Offline
|
|
OK I did that but I'm still not sure exactly how I use this thing though?
|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Oct 2001
Location: Oxford, OH
Status:
Offline
|
|
Jeez. I still can't get this Perl module to work. I give up. Put the DoAppleScript() subroutine right in the script:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
#!/usr/bin/perl
# itunesisplaying - prints the current song playing in iTunes
sub DoAppleScript {
my $result = $^W ?
`/usr/bin/osascript -ss <<'TPIRCSASO'\n$_[<font color = blue>0</font>]\nTPIRCSASO`:
`/usr/bin/osascript -ss <<'TPIRCSASO' <font color = blue>2</font>>/dev/null\n$_[<font color = blue>0</font>]\nTPIRCSASO`;
chomp $result;
return $result;
}
my $currentsong = DoAppleScript( qq{
tell application <font color = red>"iTunes"</font>
{ artist of current track, name of current track, album of current track, year of current track as string }
end tell
} );
print <font color = red>"iTunes is playing: $currentsong\n"</font>;
</font>[/code]
Now for some cleaner AppleScript code... You wanna help me on that one? I don't know much about AppleScript.
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Jan 2000
Location: Stoneham, MA, USA
Status:
Offline
|
|
#!/usr/bin/perl
use MacPerl;
sub DoAppleScript { my $result = $^W ? `/usr/bin/osascript -ss <<'TPIRCSASO'\n$_[0]\nTPIRCSASO`; `/usr/bin/osascript -ss <<'TPIRCSASO' 2>/dev/null\n$_[0]\nTPIRCSASO`; chomp $result; return $result;} my $currentsong = DoAppleScript( qq{ tell application "iTunes" artist of current track end tell} );print ("Content-type: text/html\n\n");print ("$currentsong\n");
[ 03-02-2002: Message edited by: l008com ]
[ 03-02-2002: Message edited by: l008com ]
|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Oct 2001
Location: Oxford, OH
Status:
Offline
|
|
Sorry, but I can't read your code.
Make sure that you did the UBB code right !
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Jan 2000
Location: Stoneham, MA, USA
Status:
Offline
|
|
EVERYTHIGN I TRY TO GET MY CODE UP HERE MESSES IT UP!!!! HOW DID YOU DO IT?
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Jan 2000
Location: Stoneham, MA, USA
Status:
Offline
|
|
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>#!/usr/bin/perluse MacPerl;sub DoAppleScript { my $result = $^W ? `/usr/bin/osascript -ss <<'TPIRCSASO'\n$_[<font color = blue>0</font>]\nTPIRCSASO`; `/usr/bin/osascript -ss <<'TPIRCSASO' <font color = blue>2</font>>/dev/null\n$_[<font color = blue>0</font>]\nTPIRCSASO`; chomp $result; return $result;} my $currentsong = DoAppleScript( qq{ tell application <font color = red>"iTunes"</font> artist of current track end tell} );print (<font color = red>"Content-type: text/html\n\n"</font>);print (<font color = red>"$currentsong\n"</font>);</font>[/code]
|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Oct 2001
Location: Oxford, OH
Status:
Offline
|
|
In your Perl script, don't say use MacPerl; because we put the subroutine that runs AppleScript right in the Perl script, we won't be dealing with that module.
And for some reason, the osascript program doesn't want to run AppleScripts via CGI, but here's the program that can be run from the command line. I've given up on the AppleScript formatting, so I did the formatting in Perl instead. Long and dirty, I say.
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
#!/usr/bin/perl
# itunesisplaying - prints the current song playing in iTunes
sub DoAppleScript {
my $result = $^W ?
`/usr/bin/osascript -ss <<'TPIRCSASO'\n$_[<font color = blue>0</font>]\nTPIRCSASO`:
`/usr/bin/osascript -ss <<'TPIRCSASO' <font color = blue>2</font>>/dev/null\n$_[<font color = blue>0</font>]\nTPIRCSASO`;
chomp $result;
return $result;
}
my $song = DoAppleScript( qq{
tell application <font color = red>"iTunes"</font>
name of current track
end tell
} );
my $artist = DoAppleScript( qq{
tell application <font color = red>"iTunes"</font>
artist of current track
end tell
} );
my $album = DoAppleScript( qq{
tell application <font color = red>"iTunes"</font>
album of current track
end tell
} );
#print <font color = red>"Content-type: text/html\n\n"</font>;
print <font color = red>"iTunes is currently playing $song by $artist on the album $album\n"</font>;
</font>[/code]
copy and paste and see if it works this time 
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Jan 2000
Location: Stoneham, MA, USA
Status:
Offline
|
|
text written to my html document...
"iTunes is currently playing by on the album"
I tried putting the applescript statements in a script handler, then in a run handler in a script handler, still no go.
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Jan 2000
Location: Stoneham, MA, USA
Status:
Offline
|
|
Is that 5th line, excluding lines that are blank, supposed to end with a ';' instead of a ':'?
|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Oct 2001
Location: Oxford, OH
Status:
Offline
|
|
No, it's not a mistake. It's a shorcutted if...then statement, saying "if perl was called with the -w switch (warnings) then execute this code, if not, execute this code." I don't know why that's there.
Here's my version of the subroutine that I can actually understand, and I'm also using the -sh switch on the osascript program, which puts the output in "human-readable form", which doesn't put the output in quotes (as opposed to the -ss switch):
sub AppleScript {
my $result = `/usr/bin/osascript -sh << 'END'\n$_[0]\nEND`;
chomp $result;
return $result;
}
The osascript program doesn't want to be executed from CGI scripts, I presume that the permissions of the program don't allow it (even though it's readable and executable by everyone ???).
So, I'm working on a script that will write to a file every x seconds or so the current information. If you notice, the execution of the osascript program is horribly slow (maybe because it has to compile and execute the AppleScript source code every time it runs... hmmm... you could put the scripts to seperate file, etc., but that wouldn't look good.)
You could also make it FTP to your webserver, instead of doing an SSI from your webserver to your mac, because if the mac is turned off, the SSI will fail and look bad. Also, I'm already running a linux server on port 80 of my cable connection
But wait-- I could do a config SSI maybe to configure the error to read "Sorry, iTunes isn't turned on." or something of the sort. And as for the server issue, I could make my mac run on port 81 or something instead... this is worth investigating.
I'm writing my thoughts...
[ 03-02-2002: Message edited by: zekemon6 ]
|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Oct 2001
Location: Oxford, OH
Status:
Offline
|
|
and there's a serious problem with the HTML of this thread... moderators: can your fix it please? The text goes always in one line and doesn't look like paragraphs...
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Jan 2000
Location: Stoneham, MA, USA
Status:
Offline
|
|
The HTML problem is because the CODE tags kept making my code fragments into one long line. you're thought you were writing didn't make sense to me at all, except that part about iTunes not running, I planed on inserting another applescript before that one that first checked to see if itunes was even running before it did this part.
|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Oct 2001
Location: Oxford, OH
Status:
Offline
|
|
OK, here's my final code. I am having problems with the SSI stuff though, it seems I can't include a file from another server onto my website... I'll work that out later.
I give up. Go http://unixgen.muohio.edu/~runyonr/c...esisplaying.pl for the script, it is pointless to keep trying to make this ubb code to work...
Start the script and every 30 seconds, it updates the itunesisplaying.html. When iTunes quits, the program quits after writing that iTunes had quit in the itunesisplaying.html file.
You can also quit the program by Ctl-C, and when the program recieves ctl-C, it says that iTunes isn't running in the itunesisplaying.html file, then quits.
Updates to the script will follow.
I'll be happy to answer any questions, the comments in the script say stuff on their own, but my writing isn't very clear.
[ 03-02-2002: Message edited by: zekemon6 ]
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Jan 2000
Location: Stoneham, MA, USA
Status:
Offline
|
|
thats all pretty cool but i want all that to run in a cgi, so it only runs once, when my page loads.
|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Oct 2001
Location: Oxford, OH
Status:
Offline
|
|
Well, as I said before, the /usr/bin/osascript program doesn't seem to like being run from CGI.
Anyone know how to get around this?
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Jan 2000
Location: Stoneham, MA, USA
Status:
Offline
|
|
I should log in as root :-P
|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Oct 2001
Location: Oxford, OH
Status:
Offline
|
|
Originally posted by l008com:
<STRONG>I should log in as root :-P</STRONG>
That wouldn't help, users using executing the CGI (or the Apache daemon) would have to be root. The program is executable by EVERYONE anyway, so there should be NO problems ! ! ! 
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Jan 2000
Location: Stoneham, MA, USA
Status:
Offline
|
|
did you try making it readable and writable by everyone too?
|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Oct 2001
Location: Oxford, OH
Status:
Offline
|
|
Originally posted by l008com:
<STRONG>did you try making it readable and writable by everyone too?</STRONG>
It only needs to be readable and executable by all, and it is.
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Jan 2000
Location: Stoneham, MA, USA
Status:
Offline
|
|
yes, but did you try anyway? Just for shïts and giggles?
|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Oct 2001
Location: Oxford, OH
Status:
Offline
|
|
Originally posted by l008com:
<STRONG>yes, but did you try anyway? Just for shïts and giggles?</STRONG>
I shat, I giggled, and chmod 777 /usr/bin/osascript and it still doesn't work.
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Jan 2000
Location: Stoneham, MA, USA
Status:
Offline
|
|
Hmmmmm
You didn't forget to wipe did you?
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Jan 2000
Location: Stoneham, MA, USA
Status:
Offline
|
|
Let me just bump this thread to the top to see if theres anyone out there that can help me put this one to bed as well.
|
|
|
| |
|
|
|
 |
|
 |
|
Registered User
Join Date: Nov 2001
Location: Jersey
Status:
Offline
|
|
What does this do, exactly?
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
my $result = $^W ?
`/usr/bin/osascript -ss <<'TPIRCSASO'\n$_[<font color = blue><font color = blue>0</font></font>]\nTPIRCSASO`:
`/usr/bin/osascript -ss <<'TPIRCSASO' <font color = blue><font color = blue>2</font></font>>/dev/null\n$_[<font color = blue><font color = blue>0</font></font>]\nTPIRCSASO`;
</font>[/code]
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|