Jump to content

Simple AppleScript Help


22 posts in this topic

Recommended Posts

Hello all,

I am brand new to AppleScripting, but I don't have the time to dedicate right now to actually learning the language - I just need to get one script done. Here is what it needs to do:

1. Once the program is launched, a 16 second audio clip will play.

2. All currently running programs will be forced to quit with no save options.

3. The computer will be shut down.

Seeing as I have literally never scripted before, what do you think the best way of going about this is?

Thanks

Link to comment
Share on other sites

What about QuickTime? Every Mac has QuickTime installed. Or do you not want the user to see the QuickTime window. if you don't, then making a Cocoa app is the only way I can think of that will do what you want.

Link to comment
Share on other sites

OK then use this script.

set audioFile to "/path/to/your/audio/file.mp3"

tell application "QuickTime Player"
activate
open audioFile
end tell

tell application "Finder"
set visible of process "QuickTime Player" to false
end tell

delay 16

tell application "System Events"
set theProcesses to every process whose visible is true and name is not "Finder" and name is not (name of me)
end tell

repeat with i from 1 to (count of theProcesses)
set thisProcess to item i of theProcesses
try
	tell application (name of thisProcess) to quit without saving
end try
end repeat

tell application "Finder" to shut down

Don't forget to edit the first line to point to the audio file you want QuickTime to play and to save it as an application.

Link to comment
Share on other sites

Try using this.

set audioFile to "/path/to/your/audio/file.mp3"

tell application "QuickTime Player"
activate
open audioFile
play the front document
end tell

tell application "Finder"
set visible of process "QuickTime Player" to false
end tell

delay 16

tell application "System Events"
set theProcesses to every process whose visible is true and name is not "Finder" and name is not (name of me)
end tell

repeat with i from 1 to (count of theProcesses)
set thisProcess to item i of theProcesses
try
	tell application (name of thisProcess) to quit without saving
end try
end repeat

tell application "Finder" to shut down

 

What OS are you running? Also note that the computer will only shutdown if you save this script as an application and run the app. If you just click "Run" in Script Editor it wont work.

Link to comment
Share on other sites

I works fine for me. AppleScript is can be so picky sometimes.

Try this one.

 

set audioFile to "Macintosh HD:Users:yourUserName:pathTo:theAudioFile.aif" as alias

tell application "Finder" to set theName to (name of audioFile)

tell application "QuickTime Player"
activate
open audioFile
play document theName
end tell

delay 16

tell application "System Events" to set theProcesses to every process whose visible is true and name is not "Finder" and name is not (name of me)

repeat with i from 1 to (count theProcesses)
set thisProcess to item i of theProcesses
try
	tell application (name of thisProcess) to quit without saving
end try
end repeat

tell application "Finder" to shut down

Link to comment
Share on other sites

Yes there is.

Change the top line in the script to this…

set audioFile to ((path to me as string) & "Contents:Resources:" & "audioFile.aif") as alias

 

When you save the script select "Application bundle" as the file format.

20080503-m5fhbwbpg7bnfc59c1ypn6ei3p.jpg

 

In the Finder, right click the newly saved app and select "Show Package Contents". Navigate to "Contents>Resources" and place your audio file there (inside the "Resources" folder).

 

Now it's self contained.

Link to comment
Share on other sites

OK, the program now reads:

set audioFile to ((path to me as string) & "Contents:Resources:" & "audio.wav") as alias
tell application "Finder" to set theName to (name of audioFile)

tell application "QuickTime Player"
activate
open audioFile
play document theName
end tell

delay 16

tell application "System Events" to set theProcesses to every process whose visible is true and name is not "Finder" and name is not (name of me)

repeat with i from 1 to (count theProcesses)
set thisProcess to item i of theProcesses
try
	tell application (name of thisProcess) to quit without saving
end try
end repeat

tell application "Finder" to shut down

However, when I launch it, it says "CAn't get document "audio.wav" of application "Quicktime Player". Oddly, once I click OK on that error message, the file is present (not autoplaying) and I can press play manually. The script aborts though.

'

Link to comment
Share on other sites

Argh. And you though this was going to be easy. :huh:

OK 6th time's the charm. Replace the whole QuickTime tell block with this new one.

tell application "QuickTime Player"
activate
open audioFile
set openDocuments to 0
set timer to 0
repeat while (count openDocuments) < 2 and timer < 10
	set openDocuments to the name of every document
	repeat with i from 1 to (count openDocuments)
		play document (item i of openDocuments)
	end repeat
	set timer to (timer + 1)
	delay 1
end repeat
end tell

Link to comment
Share on other sites

Error: Can't get document "promo.mov" of application "Quicktime Player". Audio does play though. Seems to be a problem with the Quicktime Guide that launches. If you can get the content guide not to load, this might work. 7th time?

Link to comment
Share on other sites

Does the content guide at least close now?

tell application "QuickTime Player"
activate
delay 2
set openDocuments to (every document)
repeat with i from 1 to (count openDocuments)
	close (item i of openDocuments)
end repeat
open audioFile
play document "audio.wav"
end tell

Link to comment
Share on other sites

 Share

×
×
  • Create New...