Tcl 和 applescript 的集成

来自:http://lists.apple.com/archives/AppleScript-Users/2006/Oct/msg00652.html

Re: Integration of Apple- and Tcl- script (Re: Applescript equivalent of $0 in shell script)

  • SubjectRe: Integration of Apple- and Tcl- script (Re: Applescript equivalent of $0 in shell script)
  • From: Philip Aker <email@hidden >
  • Date: Thu, 26 Oct 2006 20:50:46 -0700
  • Delivered-to: email@hidden
  • Delivered-to: email@hidden
Hello Hiroshi,

It becomes very slow when CPU usage is high. (I record FM-radio in my Mac and often run batch jobs.)
When I wrote this script I thought of something like TclApplescript, but never dreamed of such things really exists!
I thought Tcl is only for UNIX and did not even search the net.

Inspired by your code examples and TclApplescript, I'm thinking of rewriting this script to make things faster.
There seems to be two options for this:

(1) Transfer most of the functions in this script to 'iTunes_Monitor' and simplify this script.
This script will only call functions in 'iTunes_Monitor' on the events from the USB device.
I've partially done this on 'pause event' from the USB device where it calls a function in 'iTunes_Monitor'
to 'remember current track and playlist'

(2) Rewrite this script using TclApplescript library.

If I can integrate Apple- and Tcl- script, I think (2) would be better.
How do you think?
 
 
TclApplescript is installed on OS X 10.4 and later in /System/Library/ Tcl but I think it is also coming with TclAquaBI (BatteriesIncluded) releases for 10.2 and newer. If you do not have it already, it is available from here but I do not know if this version is good for OS 10.3.9.

http://sourceforge.net/project/showfiles.php? group_id=108367&package_id=117071

You can put it in any of the package paths. On my machine they are:

% set tcl_pkgPath
/System/Library/Frameworks/Tcl.framework/Versions/8.4/Resources/ Scripts ~/Library/Tcl /Library/Tcl /Network/Library/Tcl /System/ Library/Tcl ~/Library/Frameworks /Library/Frameworks /Network/Library/ Frameworks /System/Library/Frameworks

 
 
If using only osascript then here:

DbgMsg 1 "next_track = $next_track"
set iTunes_status [exec sh -c "osascript -e 'tell application \"iTunes\" to get player state' "]
sh -c is not necessary.

set iTunes_status [exec /usr/bin/osascript -e {tell application "iTunes" to get player state}]

with TclApplescript:

###
package require Tclapplescript
rename AppleScript ascr

set iTunes_status [ascr execute {tell application "iTunes" to get player state}]
###

You can use a whole script:

###
package require Tclapplescript
rename AppleScript ascr

set iTunes_status [ascr execute {
tell application "iTunes"
	set pstate to get player state
	if (pstate is stopped) then
		return pstate
	end if
	return pstate
end tell
}]
puts $iTunes_status
###


Especially, I want to know how to integrate Apple- and Tcl- script,
i.e., referring Tcl variables in TclApplescript and vice versa.
 
 
###
package require Tclapplescript
rename AppleScript ascr

set pstate [ascr execute {
set pstate to missing value
tell application "iTunes"
	set pstate to get player state
	if (pstate is stopped) then
		-- many lines of code here
		return stopped
	end if
end tell
return pstate
}]

set message "\"iTunes state is: $pstate\""

append script {
tell application "Finder"
	activate
	display dialog }
append script $message
append script { buttons {"OK"} default button "OK" with icon note
end tell
}

ascr execute $script
###

Hope this will help.

Philip

On 2006-10-25, at 06:33:59, Hiroshi T. wrote:

Hi, Philip. Thanks for your Applescript code examples.

I searched the net for TclApplescript but could not find good code examples.
Especially, I want to know how to integrate Apple- and Tcl- script,
i.e., referring Tcl variables in TclApplescript and vice versa.
If you know of any site that provides Tcl code examples with TclApplescript, please let me know.

As for your article,
creating an 'entrance script' for each 'one-click job' would be the simplest solution for Panther,
at least for now. But I'd still want to stick to one-file solution.
Instead I made use of your concept of 'entrance script' for simpler jobs
such as 'remember current track and playlist' and 'retrieve remembered track and playlist'.
These scripts call functions in my Applescript application 'iTunes_Monitor' that polls
iTunes status for various purposes such as implementing 'STOP TRACK' and 'VOLUME DOWN TRACK'.
I am the guy that asked about 'iTunes hook' in this ML and compromised to create 'iTunes_Monitor'
for polling iTunes instead of receiving events from iTunes.

Currently I run another monitor program for iTunes that implements simple radio-remote controller for iTunes.
It is written in Expect/Tcl script. It monitors hand-made USB device that receives radio signal from radio
transmitters diverted from wireless door chime. Since I knew nothing about TclApplescript,
I embedded ugly commands like:

proc iPlay {iComm} {
    global iTunes_status
    global next_track
    global downward
    global cur_track
    global prev_track
    global timeout

#catch {exec say "yes"}

DbgMsg 1 "next_track = $next_track"
set iTunes_status [exec sh -c "osascript -e 'tell application \"iTunes\" to get player state' "]
DbgMsg 2 "iTunes Status = $iTunes_status"

if {"$iTunes_status" == "paused" } {
                 if {$next_track == 1} {

if {$iComm == 1} {
catch {exec say "next track" }
exec sh -c "osascript -e 'tell application \"iTunes\" to next track' -e 'tell application \"iTunes\" to play'"
} else {
catch {exec say "previous track" }
exec sh -c "osascript -e 'tell application \"iTunes\" to previous track' -e 'tell application \"iTunes\" to play'"
}
} else {
if {$iComm == -1} {
catch {exec say "rewind" }
exec sh -c "/usr/local/UNIX/bin/osasubr ~hrst/MyApps/OSX/Audio/Replay_iTunes.scpt main 0"
exec sh -c "osascript -e 'tell application \"iTunes\" to play'"

} else {
#catch {exec say "play" &}
exec sh -c "osascript -e 'tell application \"iTunes\" to play'"
}

}
                      #set timeout -1
                   set timeout [expr 60 * 60]
                   DbgMsg 2 "Setting timeout to $timeout"
                   set next_track 0

} elseif {"$iTunes_status" == "playing" } {
#set timeout 30
if {$iComm == 1} {
#exec sh -c "osascript -e 'tell application \"iTunes\" to pause'"
catch {exec sh -c "osascript -e 'tell application \"iTunes_Monitor\" to count remember_current_playlist ()'" }
set timeout 10
DbgMsg 2 "Setting timeout to $timeout"
set next_track 1
} else {
exec sh -c "/usr/local/UNIX/bin/osasubr ~hrst/MyApps/OSX/Audio/Replay_iTunes.scpt main 30"

}

} else {
DbgMsg 2 "You cannot come here!"
exec sh -c "osascript -e 'tell application \"iTunes\" to playpause'"
set timeout -1
set next_track 0
}

}

It becomes very slow when CPU usage is high. (I record FM-radio in my Mac and often run batch jobs.)
When I wrote this script I thought of something like TclApplescript, but never dreamed of such things really exists!
I thought Tcl is only for UNIX and did not even search the net.

Inspired by your code examples and TclApplescript, I'm thinking of rewriting this script to make things faster.
There seems to be two options for this:

(1) Transfer most of the functions in this script to 'iTunes_Monitor' and simplify this script.
This script will only call functions in 'iTunes_Monitor' on the events from the USB device.
I've partially done this on 'pause event' from the USB device where it calls a function in 'iTunes_Monitor'
to 'remember current track and playlist'

(2) Rewrite this script using TclApplescript library.

If I can integrate Apple- and Tcl- script, I think (2) would be better.
How do you think?

On Oct 18, 2006, at 8:59 PM, Philip Aker wrote:

On 2006-10-15, at 07:44:19, Hiroshi T. wrote:

I'm not entirely clear on what you want to accomplish, but you can script application bundles easily with regular scripts:
I should have stated my intention first. That's code sharing. I wrote an Applescript to convert music file format in iTunes. There are many options for conversion and currently the script asks every option in the 'display dialog'. Some of these options are of frequent use and I want to do them by one click.My envisioned solution to implement this mechanism is to make symbolic links to the script and call them in the 'iTunes Script Menu'. For this to work, I need '$0'-equivalent in Applescript. Namely, the script is to obtain the name of file off which it is launched and changes what it does according to the file name. For example, let the name of the script be 'Convert.scpt' and let us make symbolic links to it named 'AAC.scpt', 'DeEmph.scpt', and 'Add2iPod.scpt'. Put all of them in the directory ~/Library/ iTunes/Scripts so that their file names appear in the 'iTunes Script Menu'. The script asks many options if it is launched as 'Convert' but it only de-emphasize the selected tracks if it is launched as 'DeEmph", etc.
The script must be saved as 'Script' since some of the functions in the script are called by the command 'osasubr', which can execute a function in an Applescript file from a shell script. This is necessary because 'Convert.scpt' offers 'batch option', which allows me to do batch conversion later.
Hello Hiroshi,

I see that you want to avoid those many dialog boxes. You can call subroutines in AppleScript applications from scripts. Say you make an AppleScript application bundle called "HiroConvert.app" and put in it the contents of Convert.scpt.

---
on convert(theOptions)
 -- Convert.scpt code here
end convert

on AAC(theOptions)
    set tf to theOption's batch
    set fname to theOption's name
    if (tf is true) then
            display dialog "batch" with icon note
   else
                display dialog "NO batch" with icon stop
  end if
       convert({batch:tf, creator:"Hiro", name:fname})
end AAC

on DeEmph(theOptions)
     convert({batch:false})
end AAC


Then you have script in the iTunes Script menu called "AAC". It looks something like:

tell application "HiroConvert"
       activate
        AAC({batch:false})
end tell


I'm thinking the application can do all the work of your Convert.scpt and that the script then does not need to know the path. Is that true?


There are other scripting facilities too. Tcl scripting language <x-man-page://tclsh> has AppleScript support (maybe some others like Python and Perl too).

I didn't know that. I've been heavily depending on Expect/Tcl since NEXTSTEP days. Expect/Tcl is the script of my choice.
I like your choice very much.
 
 
Philip Aker
email@hidden

_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/mailman//archives/applescript-users

This email sent to email @hidden
  • Follow-Ups:
    • Re: Integration of Apple- and Tcl- script (Re: Applescript equivalent of $0 in shell script)
      • From: "Hiroshi T." <email@hidden>
References:  
  >Comprehensive Applescript Reference Book (Re: Applescript equivalent of $0 in shell script) (From: "Hiroshi T." <email@hidden>)
  >Re: Comprehensive Applescript Reference Book (Re: Applescript equivalent of $0 in shell script) (From: Philip Aker <email@hidden>)
  >Re: Comprehensive Applescript Reference Book (Re: Applescript equivalent of $0 in shell script) (From: "Hiroshi T." <email@hidden>)
  >Re: Comprehensive Applescript Reference Book (Re: Applescript equivalent of $0 in shell script) (From: Philip Aker <email@hidden>)
  >Re: Comprehensive Applescript Reference Book (Re: Applescript equivalent of $0 in shell script) (From: "Hiroshi T." <email@hidden>)
  >Re: Comprehensive Applescript Reference Book (Re: Applescript equivalent of $0 in shell script) (From: Philip Aker <email@hidden>)
  >Integration of Apple- and Tcl- script (Re: Applescript equivalent of $0 in shell script) (From: "Hiroshi T." <email@hidden>)

  • Prev by Date: Re: Writing to file as UTF8 with BOM ?
  • Next by Date: Re: Writing to file as UTF8 with BOM ?
  • Previous by thread: Integration of Apple- and Tcl- script (Re: Applescript equivalent of $0 in shell script)
  • Next by thread: Re: Integration of Apple- and Tcl- script (Re: Applescript equivalent of $0 in shell script)
  • Index(es):
    • Date
    • Thread

你可能感兴趣的:(Tcl 和 applescript 的集成)