Reposition Quick Look Window

I wrote the script included below to scroll with Quick Look through PNG files in a folder. Absolute requirements of this script are:

  • A Finder window will not be open.
  • The script will use Quick Look to view the files.
  • I will be able to scroll through other files in the folder with the arrow keys or the mouse wheel.

My script works as I want, but I also need to reposition the Quick Look window. I suspect this is not possible but I thought I’d ask. Thanks.

set ssFolder to "Macintosh HD:Users:Robert:Screenshots:"

tell application "Finder"
	set ssFiles to (every file in folder ssFolder) as alias list
end tell

repeat with aFile in ssFiles
	set contents of aFile to (quoted form of POSIX path of aFile) & space
end repeat

do shell script "qlmanage -p " & ssFiles

the application “qlmanage” is not scriptable nor GUI scriptable

only way to get this to work is to violate your rule about using the Finder
or figure out how to use the ASObjC framework “QuickLookUI”

Here is a version that will create a Finder window of the folder requested, then select all the PNGs, then open a QuickLook window.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set ssFolder to "Mac SSD:Users:Robert:Desktop:"

tell application "System Events" to tell application process "Finder"
	if exists window "Quick Look" then
		beep
		return
	end if
end tell

tell application "System Events"
	set ssFiles to path of every file of folder ssFolder whose name extension is "png"
end tell

tell application "Finder"
	repeat with anAlias in ssFiles
		set contents of anAlias to file (contents of anAlias)
	end repeat
	set myWins to windows
	set flag to true
	repeat with i from 1 to count myWins
		if name of window 1 ≠ "Searching “Desktop”" then -- skip over desktop
			if (target of window i as text) = ssFolder then
				set flag to false
				set index of window i to 1
				set myWin to window 1
				exit repeat
			end if
		end if
	end repeat
	if flag then
		set myWin to make new Finder window
		set target of myWin to ssFolder
	end if
	set selection to ssFiles
	activate
end tell

tell application "System Events" to tell application process "Finder"
	-- set frontmost to true
	-- keystroke "y" using {command down} --> command + 16
	click menu item "Quick Look" of menu 1 of menu bar item "File" of menu bar 1
	repeat until exists window "Quick Look"
		tell me to delay 0.2
	end repeat
	tell window "Quick Look"
		--set myPos to position
		set position to {100, 100}
	end tell
end tell
1 Like

In order to move the QuickLook window, you have to use GUI scripting.
You can’t get to the “QuickLook” window with the scriptable “Finder”
so I used the GUI command since I already had the GUI tell block.
Also I don’t think you can use the “keystroke” or “key code” commands as they are part of the “System Events” dictionary, not the “Finders”

Also I prefer to use

click menu item "Quick Look" of menu 1 of menu bar item "File" of menu bar 1

instead of the keystroke command
I edited the above script to reflect this

Doing it the GUI way will continue to work even if, by accident or happenstance, the Finder is not frontmost

I also didn’t select all files in the folder. Only files of type PNG since Peavine only wanted screenshots. I used the where clause in “System Events” to only get PNGs just incase there are other files of differing types.
I use “System Events” over the “Finder” especially when using a “where” clause, since the “Finder” is horribly slow at returning the results when using them.

Thanks Robert for the script, which works well.

I edited my script (ie clean it up a little)

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set ssFolder to "Mac SSD:Users:Robert:Desktop:"

local ssFiles, anAlias, myWins, myWin, flag
tell application "System Events" to tell application process "Finder"
	if exists window "Quick Look" then
		beep
		return
	end if
end tell

tell application "System Events"
	set ssFiles to path of every file of folder ssFolder whose name extension is "png"
end tell

tell application "Finder"
	repeat with anAlias in ssFiles
		set contents of anAlias to file (contents of anAlias)
	end repeat
	set myWins to windows
	set flag to true
	repeat with myWin in myWins
		set myWin to contents of myWin
		if name of myWin ≠ "Searching “Desktop”" then -- skip over desktop
			if (target of myWin as text) = ssFolder then
				set flag to false
				set index of myWin to 1
				exit repeat
			end if
		end if
	end repeat
	if flag then
		set myWin to make new Finder window
		set target of myWin to ssFolder
	end if
	activate
	set selection to ssFiles
end tell

tell application "System Events" to tell application process "Finder"
	--set frontmost to true
	--keystroke "y" using {command down} --> command + 16
	click menu item "Quick Look" of menu 1 of menu bar item "File" of menu bar 1
	repeat until exists window "Quick Look"
		tell me to delay 0.2
	end repeat
	tell window "Quick Look"
		set myPos to position
		set position to {100, 100}
	end tell
end tell