When one feeds Finder a file to open, how does one reliably tell what app has been launched so that the resulting window can be moved and resized? This is not happening when I use:
tell application "System Events"
set theApp to (name of the first process whose frontmost is true)
end tell
The objective is to get the windows to tile with a slight horizontal and vertical offset. I’ll eventually set the “.meta” file on top, as the last window.
Here is some code I’ve been developing. It illustrates what I’m trying to do.
-- --------------------------------------------------------------------------------
-- Purpose:
-- To implement a psuedo-metadata file
--
-- Description:
-- MetaProxy is set up as the default application for a pseudo-metadatafile.
-- A typical extension is ".meta". The psuedo-medata file has an embedded tag
-- such as "#INFO". Spotlight is used to search for this tage and display all ".meta"
-- files that contain it. The user double-clicks on one of the files and this causes
-- MetaProxy to open all files, which have the same root name as the .meta file,
-- but only for the directory in which the .metafile resides.
-- --------------------------------------------------------------------------------
-- Notes:
-- The standard place to store scripts is somewhere within the Scripts folder
-- in the user's Library folder. An alias to this is returned by the command
-- 'path to scripts folder'.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use framework "Foundation"
use framework "AppKit"
on run {input, parameters}
-- --------------------------------------------------------------------------------
-- get the fully qualified filename of the ".meta" file selected in Spotlight
-- --------------------------------------------------------------------------------
set theCompletePath to (input as string)
-- --------------------------------------------------------------------------------
-- parse the complete path
-- --------------------------------------------------------------------------------
set theParsedCompletePath to parse(theCompletePath)
set thePath to item 1 of theParsedCompletePath
set theFilename to item 2 of theParsedCompletePath
set theExtension to item 3 of theParsedCompletePath
-- --------------------------------------------------------------------------------
-- Get all the files having a filename that is the same as the ".meta" file.
-- --------------------------------------------------------------------------------
tell application "System Events"
set theList to (name of files of alias (thePath) whose (name begins with theFilename & ".") and (name does not contain "." & theExtension))
end tell
-- --------------------------------------------------------------------------------
-- Initialize system parameters and counters
-- --------------------------------------------------------------------------------
set theWindowNumber to 0
set theMainScreenBounds to getMainScreenUsableBounds()
set theWindowRectangle to theMainScreenBounds
set (item 3 of theWindowRectangle) to ((item 3 of theWindowRectangle) / 2) -- Using 2 for now, as a test
set (item 4 of theWindowRectangle) to ((item 4 of theWindowRectangle) / 2) -- Using 2 for now, as a test
-- --------------------------------------------------------------------------------
-- open each document in the list
-- --------------------------------------------------------------------------------
repeat with theFile in theList
set theDocumentPath to (thePath & ":" & theFile)
try
tell application "Finder"
open file (theDocumentPath as alias)
end tell
end try
-- --------------------------------------------------------------------------------
-- get the app used to open the foregoing document
-- *** DOESN"T SEEM TO RELIABLY RETURN THE CORRECT APP NAME ***
-- --------------------------------------------------------------------------------
tell application "System Events"
set theApp to (name of the first process whose frontmost is true)
end tell
-- --------------------------------------------------------------------------------
-- calculate the apps target window position
-- --------------------------------------------------------------------------------
set theWindowNumber to theWindowNumber + 1
set (item 1 of theWindowRectangle) to ((item 1 of theWindowRectangle) + (theWindowNumber * 10))
set (item 2 of theWindowRectangle) to ((item 2 of theWindowRectangle) + (theWindowNumber * 10))
set (item 3 of theWindowRectangle) to ((item 3 of theWindowRectangle) + (theWindowNumber * 10))
set (item 4 of theWindowRectangle) to ((item 4 of theWindowRectangle) + (theWindowNumber * 10))
-- --------------------------------------------------------------------------------
-- tell the app used to move and resize its window
-- --------------------------------------------------------------------------------
tell application theApp
try
-- may need this delay loop later
-- repeat until (exists window 1 of theApp)
-- delay 0.1
-- end repeat
set bounds of window 1 to theWindowRectangle
end try
end tell
-- display dialog ("The application called, " & theApp & ", is associated with the opened file, " & theFile)
end repeat
-- --------------------------------------------------------------------------------
-- tell the app used to open the document to move and resize its window
-- --------------------------------------------------------------------------------
set theWindowNumber to theWindowNumber + 1
set (item 1 of theWindowRectangle) to ((item 1 of theWindowRectangle) + 10)
set (item 2 of theWindowRectangle) to ((item 2 of theWindowRectangle) + 10)
-- --------------------------------------------------------------------------------
-- tell the app used to open the document to move and resize its window
-- --------------------------------------------------------------------------------
-- --------------------------------------------------------------------------------
-- open the ".meta" file in TextEdit
-- --------------------------------------------------------------------------------
try
tell application "TextEdit"
open (theCompletePath as alias)
set bounds of window 1 to theWindowRectangle
end tell
end try
end run
-- --------------------------------------------------------------------------------
-- handler to parse fully qualified filenames
-- --------------------------------------------------------------------------------
to parse(x)
set thePath to ""
set theName to ""
set theExt to ""
-- save delimiters to restore old settings
set saveTID to AppleScript's text item delimiters
-- set delimiters to the delimiter used in the path
set AppleScript's text item delimiters to {":"}
-- Try getting the last item as the filename.ext
set theName to text item -1 of (x as text)
-- If that didn't work, try getting the second from the last
if theName = "" then set theName to text item -2 of (x as text)
-- set delimiters to the delimiter between the filename and extension
set AppleScript's text item delimiters to {"."}
-- If there is a filename and extension, then break it apart
if (count of text items of theName) > 1 then
set theExt to text item -1 of theName
set theName to text 1 thru text item -2 of theName
end if
-- restore delimiters to old settings
set AppleScript's text item delimiters to saveTID
-- get the path
set thePath to (text 1 thru ((offset of theName in (x as text)) - 2) of (x as text))
return {thePath, theName, theExt}
end parse
-- -------------------------------------------------------------
-- get the usable bounds of the main screen
-- -------------------------------------------------------------
on getMainScreenUsableBounds()
set |⌘| to current application
set mainScreen to |⌘|'s class "NSScreen"'s mainScreen()
set wholeFrame to mainScreen's frame()
set visibleFrame to mainScreen's visibleFrame()
-- In High Sierra, the frame rects are returned as lists instead of records, but the functions below work with either.
set x1 to (|⌘|'s NSMinX(visibleFrame)) as integer
-- The y origin in NSScreen is at the bottom of the screen. It needs to be converted to an origin at the top for AS.
set y1 to ((|⌘|'s NSMaxY(wholeFrame)) - (|⌘|'s NSMaxY(visibleFrame))) as integer
set x2 to (|⌘|'s NSMaxX(visibleFrame)) as integer
set y2 to (y1 + (|⌘|'s NSHeight(visibleFrame))) as integer
return {x1, y1, x2, y2}
end getMainScreenUsableBounds