Folder references without using "choose folder"

I’m working on a script to compare contents of two folders, one on an external drive, and one on a server.

I’m trying to remove some pain points for the end user, one of which is that “choose folder” requires the user to do a LOT of navigation to get references to the target folders; some of them are buried many layers deep. These folders and paths will always be different every time the script is run, so I can’t hard-code a path for either the source or destination folders.

The final version of this script is likely to be a droplet, so that seems like the easiest way to get the first reference, the one to the source folder. Is there something similar I can do to get the second reference, without making the user jump through hoops? She can’t drag-and-drop both of them together since they’re on different drives/servers. If there was a way to drag-and-drop both, I could have the script sort out which was which, as the destination path will always start with the same server name.

Any ideas, kludges, workarounds etc. are welcomed!

Thanks,

Mary

I’ve done similar things.

Version one: Keep two droplet icons in the same folder. One named “Drop Source Here” (stay open main script) the other “Drop Backup Here” (also stay open, but only gets the path) User drops source folder one and is prompted to drop the destination icon on the other. The main script then gets the path from the second applet, then tells it to quit.

Version two: Use choose folder twice (source/destination), and prompt the user to drag/drop the icon onto the chose folder dialogs as they come up.

Thanks very much for those suggestions. That sounds promising. TBH, I didn’t know that you could drag a folder into the window following a “choose folder” prompt. I learned something new today.

The first way you suggested sounds more user-friendly, so I may take a run at that first.

Thanks again for the suggestions, much appreciated.

Mary

If installing a script library is an option, you might consider Shane’s Dialog Toolkit Plus. It has a path-control command which allows the user to select or drag a file to the dialog. You could use two of these, in which case the user would 1) run the script, 2) drag one file to the dialog, 3) drag the second file to the dialog, 4) select any other options, 5) select OK.

A simple example:

use AppleScript version "2.4"
use scripting additions
use script "Dialog Toolkit Plus" version "1.1.0"

set accViewWidth to 300
set {theButtons, minWidth} to create buttons {"Cancel", "OK"} default button 2
set {pathControlOne, pathLabelOne, theTop} to create labeled path control (POSIX path of (path to documents folder)) bottom 0 control width accViewWidth label text "Drag the second file here:"
set {pathControlTwo, pathLabelTwo, theTop} to create labeled path control (POSIX path of (path to desktop folder)) bottom (theTop + 12) control width accViewWidth label text "Drag the first file here:"
set allControls to {pathControlOne, pathLabelOne, pathControlTwo, pathLabelTwo}
set {buttonName, controlsResults} to display enhanced window "File Compare" acc view width accViewWidth acc view height theTop acc view controls allControls initial position {1200, 100} buttons theButtons with align cancel button
set {fileOne, fileTwo} to {item 1, item 3} of controlsResults

https://latenightsw.com/freeware/

1 Like

Thanks, peavine,

I will definitely take that for a test drive. That looks very user-friendly, with easily-understood directions, which is what I’m after. I should take a look at what else is in that pack that might be useful elsewhere. I’d certainly trust anything from Shane.

Thanks for your help!

Mary

I understand that subfolders can vary, but I would like to note that if the folder hierarchy does not change (which is the most optimal organization for a good working environment), then there is an easier solution - use the default location option that the choose folder command has.

You specify a folder (the default location where the user should look for the subfolder they want), and the dialog takes the user where they want to go.

No need additional software, click-clucks, drag and drops, and droplets.

Practical example demonstrating choosing some subfolder from very deep location:


set userTempFolder to path to temporary items from user domain

try
	tell application "Finder" to make new folder at userTempFolder ¬
		with properties {name:"JustCreatedSubfolder"}
end try

choose folder with prompt "Choose some subfolder" default location userTempFolder
1 Like

Thanks, KniazidisR,

That’s good to know for other applications. In my specific use case, the user still would have to dig through a lot of subfolders in both the source and destination locations to get at what they need, and that’s going to change pretty much every time they use it.

Also, I misspoke above, re: Shane’s script library. I was thinking the Dialog Toolkit was part of the pack, but I see it’s a separate item that has been updated since I installed (and then completely forgot about) it a few years ago. I will do some digging into that, but that looks like just what we need.

Thanks again, everybody!

I tried @peavine’s script just now, and it seems to be exactly what you need.

Actually, I like it so much I may adapt it for my scripts.

Well done, @peavine!

A little follow-up on this: I’m having an odd problem using Dialog Toolkit that’s only happening with folders that are located on Box Drive for only users who are running Big Sur.

I have a dialog box for a user to choose two folders to process and compare contents. They can either navigate to the folders, or drag-and-drop them into the window. In either case, the names of the chosen folders show in the window until the OK button is pressed. Folders could be on a user’s hard drive, on a mounted external drive, or in Box Drive.

It works flawlessly for all sources if I’m on a machine running Monterey, but several of my users are on Big Sur, which is not displaying the folder name when the folder is in Box Drive. It does display correctly when the folder is from another source. In either case, it does capture the chosen paths and the rest of the script runs correctly, but instead of displaying the name, it shows “Choose…” in the window where the name should be.

I thought it might be something to do with Box’s change to use FPE mode with OS 11.5 and above, but I’ve verified that we’re all running in FPE mode. (See https://support.box.com/hc/en-us/articles/4407737091219-Using-Box-Drive-on-macOS-File-Provider-Extension-FPE-mode.)

Anybody got any thoughts about what may be causing the folder name to be returned incorrectly in just this specific location? It’s a cosmetic issue, the script still runs right, but I’d sure like to fix it if I can.

As for the drag and drop.

You can have a stay-open app, and have the first folder dropped put into source folder variable. Then wait for second drop to add to destination folder variable, then check to see if both variables are full, then do your thing! No need for 2 icons

You can even have the app notify the user by either dialog or with the say command what was dropped and what is needed.

Here is an example stay-open app

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

property sourceDir : missing value
property destDir : missing value

global timer

on run
	
end run

on open (filenames)
	local tid, fname, shortname, dirname, statement, flag
	set {tid, text item delimiters, flag} to {text item delimiters, ":", false} --set text item delimiters to ":" -- set flag to false
	set fname to (item 1 of filenames) as text
	
	if last text item of fname = "" then -- test is folder
		set shortname to text item -2 of fname
		if sourceDir is missing value then
			set sourceDir to fname
			set statement to "Source "
		else if destDir is missing value then
			set destDir to fname
			set statement to "Destination "
			set flag to true
		end if
		--set statement to statement & " Folder is " & shortname
		if (count (text items of fname)) > 2 then -- it's a folder
			set statement to statement & " Folder is " & shortname & ", in folder " & text item -3 of fname
		else -- it's a disk
			set statement to statement & " Folder is Disk, " & shortname
		end if
		say statement
		if flag then -- do your stuff here
			beep 2
			set sourceDir to missing value -- reset if you want to do again
			set destDir to missing value
		end if
	else
		say "Item dropped was not a folder!"
	end if
	set text item delimiters to tid
	set timer to current date
end open

on idle
	try
		timer
	on error
		set timer to current date
	end try
	if ((current date) - timer) > 30 then quit -- quits after 30 seconds of inactivity
	return 10
end idle
1 Like