Quick look using a defined filepath

I am attempting to write a script to have the finder preview a file with a defined path using quick look. It is opening a random file, not the one I have set.

Thanks
David

tell application “Finder”
activate
set filepath to “Users/david/Desktop/myfilehere”
tell application “System Events” to key code 49
end tell

set filepath to "Users/david/Desktop/myfilehere"

isn’t doing anything. You’re setting a variable to a file path, then not doing anything with that variable.

The only thing your script does is bring Finder to the front, then press the [space] key.

I think this is what you’re trying to do:

tell application "Finder"
	activate
	reveal alias "Macintosh HD:Users:david:Desktop:myfilehere"
end tell
delay 0.1
tell application "System Events" to key code 49

Although you can avoid revealing the file in the Finder and the clumsiness of using keycodes, and go to Spotlight Preview directly using qlmanage in the shell. Here’s a proof of concept script I wrote testing that functionality to check with users if they’ve selected the correct files to upload, that you could update, if you want to use it.

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

set file1 to "[put a POSIX file path here]"
set file2 to "[put another POSIX file path here]"
set filelist to {file1, file2}
set rightFiles to quicklook(filelist, false)

on quicklook(filelist, dialogText)
	if dialogText is false then set dialogText to "Are these the right files?"
	set shellFileString to ""
	repeat with aFile in filelist
		set shellFileString to shellFileString & "'" & aFile & "' "
	end repeat
	-- run script "do shell script \"qlmanage -p '" & file1 & "' '" & file2 & "'  > /dev/null 2>&1 &\""
	do shell script "qlmanage -p " & shellFileString & " > /dev/null 2>&1 &"
	set isFrontmost to false
	repeat until isFrontmost is true
		delay 0.1
		tell application "System Events"
			tell process "qlmanage"
				activate
				set frontmost to true
				set isFrontmost to frontmost
			end tell
		end tell
	end repeat
	tell application "System Events"
		keystroke return using command down
		tell process "qlmanage"
			display dialog dialogText buttons {"No, try again", "Yes, proceed"} default button "Yes, proceed"
			if button returned of the result is "No, try again" then
				set rightFiles to false
			else
				set rightFiles to true
			end if
		end tell
	end tell
	set isFrontmost to false
	repeat until isFrontmost is true
		delay 0.1
		tell application "System Events"
			tell process "qlmanage"
				activate
				set frontmost to true
				set isFrontmost to frontmost
			end tell
		end tell
	end repeat
	tell application "System Events"
		keystroke "q" using command down
	end tell
	return rightFiles
end quicklook