I use the same “bounds” location/sizing values for setting Preview-based windows as I do for Word and TextEdit. It seems that the text-based windows turn out wider than the image-based ones. I also note that Preview seems to change the scale of JPG pictures, but leaves others alone (i.e, such as TIFF).
Has anyone noticed this behaviour and can give me a bit of an explanation on what’s happening?
If anyone can tell me if there’s an easy fix, it would be appreciated.
I opened a JPG file in Preview and a text document in TextEdit and ran the script shown below. Afterwards, both the Preview and TextEdit windows were identical both as to position on the screen and size.
tell application "Preview" to tell window 1
set bounds to {480, 33, 1440, 888}
end tell
tell application "TextEdit" to tell window 1
set bounds to {480, 33, 1440, 888}
end tell
Apps that are not scriptable may not respond to the bounds command and in that instance System Events or some other option has to be used, as in the following:
tell application "System Events"
tell process "FSNotes"
set position of window 1 to {480, 33}
set size of window 1 to {960, 855}
end tell
end tell
You may want to post the portion of your script that is not working correctly.
Here is the code that I’m running. It seems as if some apps react differently to bounds.
-- --------------------------------------------------------------------------------
-- 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
-- --------------------------------------------------------------------------------
-- set the current windows position parameters
-- --------------------------------------------------------------------------------
set theCurrentWindow to {200, 200, 700, 700}
-- --------------------------------------------------------------------------------
-- 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
-- --------------------------------------------------------------------------------
-- open each in turn
-- --------------------------------------------------------------------------------
set theInstantiationOrder to ""
repeat with theFile in theList
set theCurrentCompletePath to (thePath & ":" & theFile)
set theCurrentExtension to item 3 of parse(theCurrentCompletePath)
set theCurrentApp to getDefaultApp(theCurrentExtension)
tell application theCurrentApp
set theInstantiationOrder to theInstantiationOrder & ", " & theFile
open file (theCurrentCompletePath)
-- --------------------------------------------------------------------------------
-- move and resize the window
-- --------------------------------------------------------------------------------
set bounds of window 1 to theCurrentWindow
set (item 1 of theCurrentWindow) to ((item 1 of theCurrentWindow) + 25)
set (item 2 of theCurrentWindow) to ((item 2 of theCurrentWindow) + 25)
set (item 3 of theCurrentWindow) to ((item 3 of theCurrentWindow) + 25)
set (item 4 of theCurrentWindow) to ((item 4 of theCurrentWindow) + 25)
-- --------------------------------------------------------------------------------
-- scale images to fit the window
-- --------------------------------------------------------------------------------
-- set myType to do shell script "file --mime-type -br " & (quoted form of POSIX path of (theCurrentCompletePath))
-- if myType contains "image" then
-- [code to scale the image to the size of the window]
-- end if
activate
end tell
end repeat
-- --------------------------------------------------------------------------------
-- open the ".meta" file in TextEdit
-- --------------------------------------------------------------------------------
tell application "TextEdit"
set theInstantiationOrder to theInstantiationOrder & ", " & theFilename & "." & theExtension
open (theCompletePath)
set bounds of window 1 to theCurrentWindow
activate
end tell
-- display dialog (theInstantiationOrder)
display dialog ("DONE")
return input
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 default app for a given filename extension
-- -------------------------------------------------------------
on getDefaultApp(pExtStr)
# Based on @ShaneStanley's script & enhancements
# http://forum.latenightsw.com/t/how-do-i-get-the-default-app/830/2
# 2017-11-26
set thePath to (POSIX path of (path to temporary items)) & "temp." & pExtStr
set nsCurApp to current application
--- Create a Temp File with Extension ---
nsCurApp's NSFileManager's defaultManager()'s createFileAtPath:thePath |contents|:(missing value) attributes:(missing value)
set ws to nsCurApp's NSWorkspace's sharedWorkspace()
--- Get URL of Default App for That Extension ---
set nsAppURL to ws's URLForApplicationToOpenURL:(nsCurApp's |NSURL|'s fileURLWithPath:thePath)
--- Get the FileName of the App ---
set {nsResult, nsAppName, nsError} to nsAppURL's getResourceValue:(reference) forKey:(nsCurApp's NSURLLocalizedNameKey) |error|:(reference)
--- Get Just the Root Name of the App ---
set appName to nsAppName as text
set appName to text 1 thru ((offset of "." in (appName as text)) - 1) of appName
return appName
end getDefaultApp
GG. My knowledge of Applescript is somewhat limited and I do not fully understand how your script is intended to work. I use the window bounds command in a number of scripts with numerous apps (including Preview and TextEdit) and it does work as expected. I do not have Word on my computer and can’t speak to that app. Sorry I can’t be of more help.