Silently checking if a running app can make windows

Is there a way of silently checking if a running application process can make windows?
I tried:

-- with  "System Preferences" running
set checkName to "System Preferences"
checkCurrentApp(checkName)
on checkCurrentApp(currentAppName)
	try -- in case there is not a menu "File" or for any other erro here fail silently, just label the app as not working
		tell application "System Events" to tell application process (currentAppName) to set menuItemCreatesNewWindow to (get name of menu item 1 of menu 1 of menu bar item "File" of menu bar 1)
		if (menuItemCreatesNewWindow as text) contains "Window" or (menuItemCreatesNewWindow as text) is "New" then
			-- log "I got this far" -- I tried:  (**)
			tell application (currentAppName) to set startWinCount to (count (windows whose visible is true))
			tell application "System Events" to tell application process (currentAppName) to click menu item 1 of menu 1 of menu bar item "File" of menu bar 1 -- try to create one
			tell application (currentAppName)
				set after1WinCreatedWinCount to (count (windows whose visible is true))
				activate -- needed for "System Events" commands below
			end tell
			if after1WinCreatedWinCount > startWinCount then -- if we have created a new window
				tell application "System Events" to keystroke "w" using command down -- try to close the window
				return true
			else
				tell application "System Events" to keystroke "z" using command down -- try to undo
				return false
			end if
		else
			return false
		end if
		-- it works but in iTunes for example it creates a new playlist and I believe it will not undo
	on error e -- e = (*System Events got an error: Can't get menu bar item "File" of menu bar 1 of application process "System Preferences".*)
		log e -- just for now
		return false
	end try
end checkCurrentApp

I would like to be able to tell without having to create a new window. This would also avoid all possible problems resulting from clicking on the first menu item of menu “File” for apps that do some undoable jobs with it.

Hello.

Maybe this link can help you?

Thanks McUsrII,

That made for some very interesting reading and learning about menu items, shortcuts and trouble with localisation.
It taught me, for example, that you cannot count on menu, menu item names nor shortcuts as they may be different in other nations. (Unless you can ask: in Italian this menu item is Nuovo, what would this be in English localisation?).

I was hoping someone would say: there is a property you can check to make sure an app can make windows.

Things I am pondering:
When you create a project in xCode you can create a Document-Based Application.
Is there a way to ask an app: Are you document based? That would go a long way to make sure a new window can be created and that there must be a menu item to create a new window.

If that is not an option then am I correct to assume:
If there is no menu File, then the app cannot create windows. – (like in “System Preferences” for example)
If there is a menu File then is its first menu item the one that could create a window. (i.e. does anyone know of apps that do not use the first menu item for this purpose?)
If it has got submenus (I suppose you can get that info) like iTunes then it cannot create windows.

I’d be very grateful if anybody could answer any of these or suggest solutions.

Hello.

If I were you, I’d create a document based, and non document based in Xcode, and then use filemerge to compare the two info.plist files. :slight_smile:

I think really there is no rules that are set in stone that applies to a document based app, versus a a non document based app. A non document based app, can still make more documents, if a programmer chose to go this ardesous way. At least I believe so.

Hi McUsrII,

I gave your idea a shot.
In my application non doc based info.plist there was no Document types entry.
In the document based one there was.
I thought I was home and dry.

I then checked iTunes and it has got it with 25 entries both with Role Viewer and Editor.
Even System Preferences has it.
So no joy there, right?

At this point I believe that my system:

 -- activate the app in question (the name of mine is in variable chosenApp) and then...
tell application "System Events" to tell process chosenApp to set startWinCount to count windows
tell application "System Events" to keystroke "n" using command key
tell application "System Events" to tell process chosenApp to set endWinCount to count windows
set canCreateWindows to endWinCount  > startWinCount
if canCreateWindows then tell application "System Events" to keystroke "w" using command key 

still seems to be the easiest way out.
The price is a little flicker on the screen when the window appears and then goes away and that the app need Assistive Devices on.

Up to now it was a simple matter to turn a checkbox on in System Preferences but now, with Mavericks, this is a problem:
The app needs to be added to a list in the Security and Privacy pane of System Preferences and its check box needs to be switched on.
When I run my app (as a stand alone) it does not even ask if you want to add it, it just quits and does nothing, so I have to code that part too. (If I save an app in AppleScript Editor from the same code it does ask and the user stands a chance to switch it on)
I found this on one of my google searches that helps (to a point). You might be interested in reading it:
http://www.macosautomation.com/mavericks/guiscripting/index.html

Thanks for your interest and help in this I really appreciate it.

With your kind of problems, I reckon you’ll need all the help you can get. :wink:

I am glad you found some sort of closure in this. And I learned something as well.