Getting file names of items dropped on droplet?

For the life of me I can’t figure out how to get the file name for items dropped on a droplet.

Can someone guide me on this?

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

on open dropped_items
	
	repeat with theItem in dropped_items
		
		-- HOW DO I GET THE FILE NAME OF theItem???
		
	end repeat
	
end open

The name of a file is a property controlled by either the Finder or System Events.

At the bottom, I’ll show how you can use them to get all the properties of a file.

But here is how you would get the name of a collection of dropped items. The commands inside the finder tell block get the name of each item and append it to a list. After the repeat loop ends, it converts the list into text and displays it in a dialogue.

use scripting additions

-- take dropped files
on open dropped_items
	set nameList to {}
	repeat with theItem in dropped_items

		tell application "Finder"
			set nom to name of theItem
			set end of nameList to nom
		end tell

	end repeat
	set text item delimiters to linefeed
	set paras to nameList as text
	display dialog paras
end open

Here are two examples of how to get Finder and System Events properties from a file.

set theFile to choose file

tell application "Finder"
	properties of theFile
end tell
set theFile to choose file

tell application "System Events"
	properties of theFile
end tell

I think I found the problem with my applet.

I am using Script Debugger to save the script as “Application (Enhanced)”. Your code above returns an error as well if saved as “Application (Enhanced)”. If I save it as “Application (Apple)” it works fine.

“Application (Enhanced)” allows me to add messaging in the script:

set progress description to "Starting 720p scaling process on item " & myCount & " of " & totalCount & " items ..."
set progress additional description to sizeStr & linefeed & fpsStr

here’s my version

use scripting additions

-- take dropped files
on open dropped_items
	local tid, nameList
	set tid to text item delimiters
	set text item delimiters to ":"
	set nameList to {}
	repeat with theItem in dropped_items
		set end of nameList to last text item of (theItem as text)
	end repeat
	set text item delimiters to linefeed
	set paras to nameList as text
	set text item delimiters to tid
	display dialog paras
end open
1 Like

You can add a progress bar without using the enhanced app type.

1 Like

Thanks all for the assistance. Here is the final script, which works saved as “Application (Enhanced)”

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use txt : script "TextLib"
use script "RegexAndStuffLib" version "1.0.6"

on open dropped_items
	
	set myCount to 0
	set totalCount to count of dropped_items
	
	local tid
	set tid to text item delimiters
	set text item delimiters to ":"
	
	repeat with theItem in dropped_items
		
		set thisFileName to last text item of (theItem as text)
		
		set myCount to myCount + 1
		set thisFilePath to POSIX path of theItem
		
		set withdHeightStr to do shell script "/usr/local/bin/ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 " & thisFilePath
		set fpsResult to do shell script "/usr/local/bin/ffprobe -v error -select_streams v:0 -show_entries stream=r_frame_rate -of default=noprint_wrappers=1:nokey=1 " & thisFilePath
		set fpsItems to regex split fpsResult search pattern "\\/"
		set fps to (item 1 of fpsItems as integer) / (item 2 of fpsItems as integer) as integer
		
		set nameStr to thisFileName & "."
		set sizeFPSStr to "Current Size: " & withdHeightStr & " --- Current FPS: " & fps & "."
		set fullStr to nameStr & linefeed & sizeFPSStr
		
		set progress description to "Starting 720p scaling process on item " & myCount & " of " & totalCount & " items ..."
		set progress additional description to fullStr
		
		try
			with timeout of 3600 seconds
				do shell script "/opt/homebrew/Cellar/fx-upscale/1.2.6/bin/fx-upscale " & thisFilePath & "  --height 720"
			end timeout
		on error number -1712
			display dialog "Sorry, unable to upscale this video."
		end try
		
	end repeat
	set text item delimiters to tid
end open

Just so you know, my script above will fail if you drop a folder or application on it.
The last text item of those would be a blank string.
I have a minor alteration if you ever need it to work on folders and/or apps.

I don’t have the fx-upscale utility so I cannot test that part but otherwise it works for me.

A couple of points though:

In case any input file has a space in its path, use ‘quoted form of’, like so:

use quoted form of set thisFilePath to POSIX path of theItem

It places single quotes around the string which will protect it from the shell.

Also, you declare the use of ‘TextLib’ but you don’t actually use it within your script. May as well delete that line.

You use the ‘RegexAndStuffLib’ library but it isn’t necessary. You can perform that processing with text item delimiters, like so:

set fpsResult to "24000/1001"
set text item delimiters to "/"
set fps to (text item 1 of fpsResult as integer) / (text item 2 of fpsResult) as integer

Finally, I don’t know what your video sources are but there are some frame rates that are not integers: any of the NTSC (eg North American or Japanese television, maybe some other places) rates are real numbers and generally —at least within ffmpeg— described with two decimal places, eg 23.98 or 29.97. Forcing any such videos to integer rates may affect syncing. The ffprobe query may return a result of ‘24000/1001’ or ‘30000/1001’ in such cases.

Here is one way this can be dealt with:

    if fpsResult is "24000/1001" then
	  set fps to 23.98
    else if fpsResult is "30000/1001" then
      set fps to 29.97
	else
      set text item delimiters to "/"
      set fps to (text item 1 of fpsResult as integer) / (text item 2 of fpsResult) as integer
	end if

It could happen at higher rates as well, eg 60000/1001. If some of your videos fall into this category then add another else if.

There is another issue with “on open” in a stay open app.
When you drop more than a couple files, it sometimes runs “on open” twice.
with some files sent to the first call, and some to the second.

I have a version that will mitigate this issue.

use scripting additions

property namelist : {}
property cc : false

-- take dropped files
on open dropped_items
	local tid, theItem, tmp
	set tid to text item delimiters
	set text item delimiters to ":"
	repeat with theItem in dropped_items
		set theItem to contents of theItem
		repeat with tmp in reverse of (text items of (theItem as text))
			set tmp to contents of tmp
			if tmp ≠ "" then
				if tmp is not in namelist then set end of namelist to tmp
				exit repeat
			end if
		end repeat
	end repeat
	set text item delimiters to tid
end open

on idle
	local tid, paras
	if cc then -- 2nd run, do your thing here
		set tid to text item delimiters
		set text item delimiters to "\"" & linefeed & "\""
		set paras to ("\"" & namelist as text) & "\""
		set text item delimiters to tid
		try
			choose from list namelist
		end try
		quit
	else
		set cc to true
	end if
	return 0.5
end idle

fx-upscaler is a simple tool to quickly upscale video using built in Mac libraries.

Thanks for the tips on the rest of the script. Its just a quick and dirty tool to help out a professor who is archiving some old lecture videos since he retired.

Thanks. Looks interesting. Don’t think my computer supports Metal. A good use for it though.