Drag Finder item into text field and get it's StringValue

Hi Geniuses,

Im working on a project where you click a button to choose a file and the file path is displayed in an editable text field next to the button. addition functions are also called when the button is pressed.

My goal is to have it set up so the user can just drag a finder into the text field which will in turn execute the same bit of code. I’ve set it up so the text field calls the same code but I’m having issues getting setting a variable to the stringValue of the text field. I also have to press the tab or return key to execute and I was wondering if there was a way for it to execute when the item was dropped.

property FilePath1 : missing value

-- this is when the button is pushed 

on SegOne_(sender)
	set segOneInfo to SelectVideo_()
	set end of reelInfo to segOneInfo
	set totalMovieFC to totalMovieFC + (item 1 of item numberOfReel of reelInfo)
	trt's setStringValue_(framesToTC(totalMovieFC, (item 2 of item numberOfReel of reelInfo)))
	FilePath1's setStringValue_(item 3 of item 1 of reelInfo)
	Duration1's setStringValue_(framesToTC(item 1 of item numberOfReel of reelInfo, item 2 of item numberOfReel of reelInfo))
end SegOne_

-- here is where you select the video file in the finder

on SelectVideo_()
	set VideoFile to choose file
	tell application "Finder"
		set vidName to the name of VideoFile
		set vidName to text items 1 thru -5 of vidName
	end tell
	activateVideo_(VideoFile, vidName)
end SelectVideo_

-- and then the video gets passed on to:

on activateVideo_(VideoFile, vidName)
	tell application "QuickTime Player"
		open VideoFile
		if numberOfReel is 1 then
			set the bounds of window 1 to {50, 50, 690, 635}
		else if numberOfReel is 2 then
			set the bounds of window 1 to {700, 50, 1320, 635}
		else if numberOfReel is 3 then
			set the bounds of window 1 to {1330, 50, 1950, 635}
		end if
	end tell
	set filePath to (POSIX path) of (VideoFile as text)
	set IDf to do shell script "/usr/local/bin/mediainfo '--Inform=Video;%FrameRate%,%FrameCount%' " & "'" & filePath & "'"
	set Vspecs to {}
	set {ASTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ","}
	set Vspecs to text items of IDf
	set FrameRate to item 1 of Vspecs
	set movieFrameCount to item 2 of Vspecs as integer
	set AppleScript's text item delimiters to ASTID
	set aReelInfo to {movieFrameCount, FrameRate, filePath, vidName}
end activateVideo_

-- id like it to go straight to the activateVideo part when you add a finder item to the text field. Kinda like this

on DragSegOne_(sender)
	set VideoFile to FilePath1's StringValue -- this part is erroring
	-- ill also have to set up my var vidname here
	activateVideo_(VideoFile, vidName)
end DragSegOne_

Thanks for the help

gee, I get to copy and paste from Apple’s ASObjC list :slight_smile:

make your script the delegate of the text field, which you can do in Interface Builder. Then you can implement the NSControl delegate method controlTextDidChange:

on controlTextDidChange_(notif)

To distinguish between text fields, you can do something like this:

on controlTextDidChange_(notif)
    if notif's object() = textFieldVar then
        -- that's the one we want

Actually, you might be better off with controlTextDidEndEditing – it depends on what you’re after.

Thanks Shane!
I’ll try those out.

Any Idea how to set a variable to the new contents of the text field.

Much Appreciated!

Get its stringValue(). It looks like you typed it with a cap s, so you’ll probably have to use |stringValue|().