Droplet bug

You probably don’t know what I’m talking about, so here’s all the players.

Here’s the launch agent:

The Label can be anything, but it should be unique. Also, you might need to restart or maybe just restart Finder. Here’s the bash script it calls:

Remember to make it executable. In Terminal "chmod +x /path/to/the/script’. Here’s the AppleScript compiled script:

property drop_folder_path : "Macintosh HD:Users:kelhome:Desktop:DropFolder:"

-- call this script from shell script
on run -- p -- parameters if needed
	set drop_folder_ref to drop_folder_path as alias
	tell application "Finder"
		set dropped_items to every item of drop_folder_ref as alias list
		activate
	end tell
	-- move the added items back to original location
	-- but first, turn off launch agent for one-shot
	-- remember to re-load at end
	--do shell script "launchctl unload /Users/kelhome/Library/LaunchAgents/com.apple.launchagent.plist"
	tell application "System Events" to keystroke "z" using command down
	-- dropped_items are now in original loacation and references should reflect that
	-- testing: look at original locations
	set string_list to {}
	repeat with this_item in dropped_items
		set end of string_list to this_item as string
	end repeat
	activate
	choose from list string_list
	-- one shot
	do shell script "launchctl unload /Users/kelhome/Library/LaunchAgents/com.apple.launchagent.plist"
end run

The question is where to re-load the launch agent. Here’s the load and unload Terminal scripts:

It’s pretty quick, but where to re-load the launch agent. I need to step back and think. Getting to one track minded.

Edited: oops, forgot to add the .2 sec. delay:

property drop_folder_path : "Macintosh HD:Users:kelhome:Desktop:DropFolder:"

-- call this script from shell script
on run -- p -- parameters if needed
	set drop_folder_ref to drop_folder_path as alias
	tell application "Finder"
		set dropped_items to every item of drop_folder_ref as alias list
		activate
	end tell
	-- move the added items back to original location
	-- but first, turn off launch agent for one-shot
	-- remember to re-load at end
	--do shell script "launchctl unload /Users/kelhome/Library/LaunchAgents/com.apple.launchagent.plist"
	tell application "System Events" to keystroke "z" using command down
	-- dropped_items are now in original loacation and references should reflect that
	-- testing: look at original locations
	delay 0.2 -- added this
	set string_list to {}
	repeat with this_item in dropped_items
		set end of string_list to this_item as string
	end repeat
	activate
	choose from list string_list
	-- one shot
	do shell script "launchctl unload /Users/kelhome/Library/LaunchAgents/com.apple.launchagent.plist"
end run

See the comment.

gl,
kel

Cleaned up the script and modified things. Here’s the launch agent:

Here’s the shell script:

Here’s the AppleScript:

property drop_folder_path : "Macintosh HD:Users:kelhome:Desktop:DropFolder:"

-- call this script from shell script
on run -- p -- parameters if needed
	set drop_folder_ref to drop_folder_path as alias
	-- get alias list of dropped items
	tell application "Finder"
		set dropped_items to every item of drop_folder_ref as alias list
		activate
	end tell
	-- move the added items back to original location and wait for update
	tell application "System Events" to keystroke "z" using command down
	delay 0.5 -- added this
	-- dropped_items are now in original loacation and references should reflect that
	my DoSomething(dropped_items)
	return
end run

on DoSomething(item_list)
	-- testing: look at original locations
	set string_list to {}
	repeat with this_item in item_list
		set end of string_list to this_item as string
	end repeat
	activate
	choose from list string_list
	return
end DoSomething

Here’s the Terminal scripts for loading and unloading in Terminal:

Still trying to figure out where and how to load besides doing it manually.

Edited: think I’ve found a way by using a helper app! :slight_smile:

Edited: it works! :smiley: Still testing, but here’s the helper AppleScript app:

repeat
	try
		do shell script "launchctl load /Users/kelhome/Library/LaunchAgents/com.apple.launchagent.plist"
		exit repeat
	on error
		-- continue repeat
		delay 2
	end try
end repeat
say "launch agent reloaded"

Save as non-stay open app. Here’s the modified DropletAppleScript:

property drop_folder_path : "Macintosh HD:Users:kelhome:Desktop:DropFolder:"

-- call this script from shell script
on run -- p -- parameters if needed
	set drop_folder_ref to drop_folder_path as alias
	-- get alias list of dropped items
	tell application "Finder"
		set dropped_items to every item of drop_folder_ref as alias list
		activate
	end tell
	-- move the added items back to original location and wait for update
	tell application "System Events" to keystroke "z" using command down
	delay 0.5 -- added this
	-- dropped_items are now in original loacation and references should reflect that
	my DoSomething(dropped_items)
	-- run helper app to reload
	ignoring application responses
		tell application "DropletHelper"
			launch
			run
		end tell
	end ignoring
	return
end run

on DoSomething(item_list)
	-- testing: look at original locations
	set string_list to {}
	repeat with this_item in item_list
		set end of string_list to this_item as string
	end repeat
	activate
	choose from list string_list
	return
end DoSomething

I think that should do it!!! :smiley: Thanks for all the help.

Edited: btw, everything needed is in this last post.

Edited: darn, there’s a bug. Now I have to debug this thing. :slight_smile: I think the delay after the undo is too low. changing the delay to 0.5 secs.

Edited: oops, already did that. Increasing to 1 sec.

Edited: added quick fix:

property drop_folder_path : "Macintosh HD:Users:kelhome:Desktop:DropFolder:"

-- call this script from shell script
on run -- p -- parameters if needed
	set drop_folder_ref to drop_folder_path as alias
	-- get alias list of dropped items
	tell application "Finder"
		set dropped_items to every item of drop_folder_ref as alias list
		activate
	end tell
	if dropped_items is not {} then -- ADDED THIS FOR BUG
		-- move the added items back to original location and wait for update
		tell application "System Events" to keystroke "z" using command down
		delay 1 -- added this
		-- dropped_items are now in original loacation and references should reflect that
		my DoSomething(dropped_items)
	end if
	-- run helper app to reload
	ignoring application responses
		tell application "DropletHelper"
			launch
			run
		end tell
	end ignoring
	return
end run

on DoSomething(item_list)
	-- testing: look at original locations
	set string_list to {}
	repeat with this_item in item_list
		set end of string_list to this_item as string
	end repeat
	activate
	choose from list string_list
	return
end DoSomething

See comments.

Edited: it is working well now. Also fairly quick. What was happening is that sometimes the scripts would run when nothing was added. So the items would jump back into the drop folder. If anyone finds a bug can you post it? Thanks. Also thinking about changing the helper to unload if the user wants to do that instead of using Terminal to unload. Also, forgot to check cpu usage, but the last time I checked it was very good.

Edited: note that there are many ways to rewrite this and a lot that is not necessary. For instance, you don’t really need the helper app. In fact, everything can be done with just the shell script I think.

gl,
kel

Update. Sorry about the short explanations, but everyone is sick with this new terrible cold virus. Still need to cook the chicken soup for everybody. :slight_smile: Also, donating recyclable items. Waiting for the picker uppers.

Don’t need the helper, but might add a toggled app to turn on/off the loading and unloading of the agent.

Here’s the AppleScript:

property drop_folder_path : "Macintosh HD:Users:kelhome:Desktop:DropFolder:"

-- call this script from shell script
on run -- p -- parameters if needed
	set drop_folder_ref to drop_folder_path as alias
	-- get alias list of dropped items
	tell application "Finder"
		set dropped_items to every item of drop_folder_ref as alias list
		activate
	end tell
	if dropped_items is not {} then
		-- move the added items back to original location and wait for update
		tell application "System Events" to keystroke "z" using command down
		delay 0.2
		-- dropped_items are now in original loacation and references should reflect that
		my DoSomething(dropped_items)
	end if
	return
end run

on DoSomething(item_list)
	-- testing: look at original locations
	set string_list to {}
	repeat with this_item in item_list
		set end of string_list to this_item as string
	end repeat
	activate
	choose from list string_list
	return
end DoSomething

Here’s the bash script:

You don’t need to load and unload until you want to do that.

Also, the agent doesn’t need the last key, because it is default. Wasn’t sure about that because of the changing in systems. Apple updated its docs. Think that was all, but if I have anything else, then will update post. Also, think I can crunch the setup more.

Edited: Also, trying to combine everything into one package. Doing some reading up on that.

Edited: also, trying to make the keystroke"z" invisible to users.

Edited: forgot about the third big job I had to do until I got the phone call to pick up materials. :confused: Done. Now just need to make the chicken soup and finish this. While I was driving it occurred to me that Python lists is more compatible with other forms of unix lists. If you return a python list to AppleScript, then it can easily be coerced to AppleScript list. The only difference is the square brackets as opposed to AppleScript curly brackets. So It is easy to pas lists between the two languages. Maybe there is something there. In fact, AppleScript will automatically coerce the square brackets. Something to think about.

Edited: I hope nobody gets what I wrote wrong. You can not pas list from unix to Applescript. The list is in string form. When you get the string in AppleScript, you need to ‘run script’ to change it for AppleScript to use as list.

Edited: e.g.

set t to "[1,2,3]"
set AppleScript_list to run script t

gl,
kel

Yeah, it is working great!

Now I can just drag my desktop items and put them into categories!

Thanks DJ for the great idea! My desktop was getting too full too quickly! Love it man. :smiley:

All I need to do is integrate the application into one place so I don’t accidentally trash a needed part of it. Reading up on bundles and packages.

Edited: oh and did I mention that I learned a lot!!! :smiley:

Later,
kel

Almost forgot where this all originated.

Thanks CMYS!!!

The idea of using folder actions led to all this! Thanks a lot!

Great idea!
kel

Think I’ve found a way to speed this up. You need to unload and reload after the throttle time (10 secs).

Edited: you can see this, because after you load for the first time It is really fast. As time goes by it takes longer no matter if you passed the 10 second mark. i.e. after the 10 second mark it still slows down. That might be what is wrong with the folder actions. It’s continuously loaded.

Edited: or, maybe use one of these:

Maybe decrease one of these to just above 10 seconds. Need to try that. My default TimeOut is 30 seconds.

Edited: btw, here’s the toggled app:

try
	do shell script "launchctl list com.apple.launchagent"
	do shell script "launchctl unload /Users/kelhome/Library/LaunchAgents/com.apple.launchagent.plist"
	say "agent unloaded"
on error
	do shell script "launchctl load /Users/kelhome/Library/LaunchAgents/com.apple.launchagent.plist"
	say "agent loaded"
end try

You could just try to load and when you get an error, then reload, but this may have advantages for expansion.