GTB GTB - 3 months ago 12
AppleScript Question

Applescript drag and drop doesn't work

I am new to Applescript, and wrote a script to securely wipe files, which brings up a file-selection dialog and confirmation, then wipes the file. However, when I tried to add drag and drop functionality, it doesn't work like everyone says it should. The icon never highlights (indicating drag and drop is working), and Finder just copies the file to the app's folder instead of dragging onto the app!

Here is the original script, which works fine (saved as "Wipe File.app"):

on run
set the_file to choose file with prompt "Select the file to wipe:"
wipe_file(the_file)
end run

to wipe_file(file_to_wipe)
set file_to_wipe to POSIX path of file_to_wipe
set ok_to_wipe to display dialog "Are you sure you want to wipe \"" & file_to_wipe & "\"?" buttons {"Cancel", "OK"} default button "Cancel"
set ok_to_wipe to button returned of ok_to_wipe
if (ok_to_wipe = "OK") then
tell application "Terminal"
activate
do script "set prompt='';cls;srm -v \"" & file_to_wipe & "\""
delay 3
close front window
set still_active to count windows
if still_active = 0 then
quit
end if
end tell
end if
end wipe_file


Then I added the following to the top. Running the script still works with the file-selection dialog, but the system never allows drag and drop!

on open the_files
repeat with the_file in the_files
wipe_file(the_file)
end repeat
end open


Everywhere I looked (Google, stack overflow) all say this approach should work, but it doesn't. I even tried removing the
on run...
block, leaving just
on open...
, but then the script does nothing at all.

EDIT

If I create a new script with the content above, and save it as an app, then copy it to the Applications folder, and paste a custom icon onto it, drag and drop works. However, the old script, which originally didn't have "on open" support, still doesn't work even after "on open" support was added to the script. So now I'm thinking that Apple must set some special attribute to indicate a script supports drag and drop, and for some reason (because it didn't have it when I first saved?) Apple didn't set that attribute for my file. Looking at Get Info and Show Package Contents for both apps, there are some strange differences:


  1. The newly-created (working) app is only 693 KB, but the old (supposedly identical, but broken) one is 9.4 MB!

  2. Inside the Contents/MacOS folder, the new (working) app has a file called "droplet", whereas the old one (broken) one has a file called "applet".



The first one is bizarre, and made me think of some sort of file corruption, but the second one is clearly the magic setting Apple uses for drag and drop. I verified that if I remove the "on open" block from the working script, and save it, Apple does not update the script icon to remove the "drop" arrow, and you can still drag and drop files onto it, but nothing happens.

So it seems that Apple decides whether a script supports drag and drop or not the first time it saves the script, and after that you get the wrong results unless save a brand new script!

Answer

You do not need to call Terminal and open window. The ‘do shell script’ opens already a shell session, in background, not visible in Terminal. You must just use it inside a 'try/end try' block to avoid script stops in case of error (like no authorization for the files !)

Also you are using ‘srm’ with option v (verbose). This option is used to display what is being done, but you do not see it because you are closing the window.

The possible issue of your script could also be when your file path contains special characters which must be escaped in shell. To avoid this, use ‘quoted form of ‘.

Script bellow is tested OK :

on run
set the_file to choose file with prompt "Select the file to wipe:"
wipe_file(the_file)
end run

on open the_files
repeat with the_file in the_files
    wipe_file(the_file)
end repeat
end open

to wipe_file(file_to_wipe)
set file_to_wipe to POSIX path of file_to_wipe
set ok_to_wipe to display dialog "Are you sure you want to wipe \"" & file_to_wipe & "\"?" buttons {"Cancel", "OK"} default button "Cancel"
if (button returned of ok_to_wipe = "OK") then
    try
        do shell script "srm " & quoted form of (file_to_wipe)
    end try
end if
end wipe_file