set current application to variable

Hey guys,

In a script to eject all disks (other than startup disk), I want to bring whatever application was in the foreground back into focus after the script executes. How can I do this?

I’ve tried:

set runningApp to current application
tell application "Finder"
	set bootDisk to name of startup disk
	set otherDisks to every disk whose (name is not bootDisk)
	activate
	repeat with myDisk in otherDisks
		try
			eject myDisk
		on error
			display dialog "There was a problem ejecting " & (myDisk as text)
			return "Failed"
		end try
	end repeat
	display dialog "All disks ejected" buttons {"Ok"} giving up after 2
end tell
tell runningApp to activate

and I’ve also tried things like this as the definition:

set runningApp to name of current application as string
set runningApp to application "\"" & runningApp & "\""

but I can’t get the desired result…

Thanks,

Tim

I’d still like a way to do what I was attempting, which was use the current application as a variable, but in the meantime, this works for my purposes (although it doesn’t bring the exact window that was being used when the service is called back into focus afterwards…):

tell application "Finder"
	set bootDisk to name of startup disk
	set otherDisks to every disk whose (name is not bootDisk) -- and (name is not "Media")
	repeat with myDisk in otherDisks
		try
			eject myDisk
		on error
			tell current application to display dialog "There was a problem ejecting " & (myDisk as text)
			return "Failed"
		end try
	end repeat
	tell current application to display dialog "All disks ejected" giving up after 2
end tell

and here’s a different one that lets you pick which disks to eject:

tell application "Finder"
		set bootDisk to name of startup disk
		set otherDisks to every disk whose (name is not bootDisk) and (name is not "MobileBackups") and (name is not "home") and (name is not "net")
		set diskNames to {}
		repeat with myDisk in otherDisks
			set end of diskNames to name of myDisk
		end repeat
		tell current application to set ejectDiskNames to choose from list diskNames with prompt "Choose disk(s) to eject" with multiple selections allowed
		if ejectDiskNames = false then
			return 1
		end if
		set ejectDisks to {}
		repeat with ejectDisk in otherDisks
			if ejectDiskNames contains (name of ejectDisk) then
				set end of ejectDisks to ejectDisk
			end if
		end repeat
		repeat with myDisk in ejectDisks
			try
				eject myDisk
			on error
				tell current application to display dialog "There was a problem ejecting " & (myDisk as text)
				return "Failed"
			end try
		end repeat
		tell current application to display dialog "Selected disk(s) ejected" giving up after 2
	end tell

and ideas for improving on these scripts?

Thanks,

Tim

current application is the current AppleScript environment which runs the script, not the frontmost application

path to frontmost application

returns the alias of the frontmost application

or


tell application "System Events"
	set frontmostApplicationName to name of 1st process whose frontmost is true
end tell

I see. I’d never guess these things, and I’ve read through (quickly) the AppleScript bible and the comprehensive guide to learning AppleScript. Neither of these provide the background necessary to know these things.

Do you know them from instruction or just by perusing the dictionaries?

Also, can you explain in a few more sentences how the current apple scripting environment differed from the front most application? They seem like they’d be the same thing…

Thanks,

Tim

Let me google that for you

I got the information from the real AppleScript bible, Matt Neuburg’s “AppleScript The Definitive Guide”

To see the difference between current and frontmost run this script


activate application "Finder"
set currentApplication to name of current application
tell application "System Events" to set frontmostApplication to name of 1st process whose frontmost is true
tell application frontmostApplication to display dialog "current application is: " & currentApplication & return & "frontmost application is : " & frontmostApplication

it makes the Finder frontmost, then it retrieves the name of current and frontmost application.
If you run the script from AppleScript Editor, AppleScript Editor is the current application.
If you run the script from the script menu, AppleScript Runner is the current application.

Hello I use something like this, but then the previous app, has had to loose focus, for it to work properly.

I send the keystroke "cycle through windows! (ctrl-F4) Before I can do that, I have to get the bundleid, (I don’t have to, but I use that). For this to work, you have to hit fn to change the volume, etc.



set fappId to getfrontAppId()

” your code that activates something else

abortNicely({bundleIdFrontApp:fappId})


on getfrontAppId() -- Returns bundleid of active app
	local frontappId
	set frontappId to ""
	tell application "System Events"
		set frontappId to bundle identifier of first application process whose frontmost is true
	end tell
	return frontappId
end getfrontAppId

on abortNicely(R) -- Returns Nothing
	
	tell application "System Events" to tell application process id (bundleIdFrontApp of R)
		key down control
		key code 118
		key up control
	end tell
	error number -128
end abortNicely

A little late, but…

I’d never seen that before. I love it!:lol:

Hi!

This may help you. :slight_smile:

MacScripter / Activate the frontmost window in the previous app. post #2

MacScripter / Referencing the current frontmost application window post #10

A simple link can be more than thousand words :smiley: You’re welcome!