Monday, December 9, 2019

#1 2019-06-27 04:55:13 pm

Budgie
Member
From:: New Zealand
Registered: 2005-02-22
Posts: 705

how to allow more than one dropped item to be deleted

hI
I use the below to delete items when dropped onto an area in my project, it will only allow me to delete one file or one folder at a time, how can I have the code allow multiple items to be deleted at once?


Applescript:

-------------------------------
on draggingEntered_(info)
return current application's NSDragOperationCopy
end draggingEntered_

on draggingExited_(info)
mainTrashWindow's makeFirstResponder_(missing value)
end draggingExited_

on performDragOperation_(info)
set pb to info's draggingPasteboard()
set theURLs to pb's propertyListForType_("NSFilenamesPboardType")
performSelector_withObject_afterDelay_("doFinderStuff:", theURLs, 0.1)
return true
end performDragOperation_
-----------------------------------------------
-- TRASH 1
-----------------------------------------------
on doFinderStuff_(theURLs)
set FileToDelete to theURLs as string as POSIX file
tell application "Finder"
delete FileToDelete
end tell
end doFinderStuff
------------------------------------------------
--
------------------------------------------------


Cheers
Budgie

“Everything’s impossible until somebody does it.” – Batman

Offline

 

#2 2019-06-27 05:55:44 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6064

Re: how to allow more than one dropped item to be deleted

You should retrieve the URLs like this:

Applescript:

set theURLs to pb's readObjectsForClasses:{current application's NSURL} options:(missing value)

And it's a bit silly to leave the job to Finder. Use NSFileManager and either trashItemAtURL:resultingItemURL:error: or removeItemAtURL:error:.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#3 2019-06-27 07:26:01 pm

Budgie
Member
From:: New Zealand
Registered: 2005-02-22
Posts: 705

Re: how to allow more than one dropped item to be deleted

thanks Shane
I got to this stage, i think it's right , as it will accept a file and trash it, but it still only trashes one file, not multiple files, did i miss something?



Applescript:

on performDragOperation:info
   set pb to info's draggingPasteboard()
   set theURLs to pb's readObjectsForClasses:{current application's NSURL} options:(missing value)
   set theURLs to pb's propertyListForType:"NSFilenamesPboardType"
   performSelector_withObject_afterDelay_("doFinderStuff:", theURLs, 0.1)
   return true
end performDragOperation:

on doFinderStuff:theURLs
   set FileToDelete to theURLs as string as POSIX file
   set fileManager to current application's NSFileManager's defaultManager()
   fileManager's trashItemAtURL:(FileToDelete) resultingItemURL:(missing value) |error|:(missing value)
end doFinderStuff:


Cheers
Budgie

“Everything’s impossible until somebody does it.” – Batman

Offline

 

#4 2019-06-27 10:07:12 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6064

Re: how to allow more than one dropped item to be deleted

You're getting URLs and the trash method wants a URL, so you just need a simple repeat loop:

Applescript:

on doFinderStuff:theURLs
   set fileManager to current application's NSFileManager's defaultManager()
   repeat with aURL in theURLs
       (fileManager's trashItemAtURL:aURL resultingItemURL:(missing value) |error|:(missing value))
   end repeat
end doFinderStuff:


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#5 2019-07-01 03:49:11 pm

Budgie
Member
From:: New Zealand
Registered: 2005-02-22
Posts: 705

Re: how to allow more than one dropped item to be deleted

thanks Shane
been trying to get this to work, but all I get back is errors, I cant seem to figure out what the problem is


just dropping one file returns the below

019-07-02 09:45:08.835698+1200 DROPTRASH[41104:17006977] *** -[DROPTRASH doFileManagerStuff:]: -[__NSCFString isFileURL]: unrecognized selector sent to instance 0x618000462640 (error -10000)



I have played around with different variations, pretty well much the same result as above

Applescript:

on doFileManagerStuff:theURLs
set fileManager to current application's NSFileManager's defaultManager()
repeat with aURL in theURLs
--try
tell the fileManager to trashItemAtURL:aURL resultingItemURL:(missing value) |error|:(missing value)
--end try
end repeat
end doFileManagerStuff:


Cheers
Budgie

“Everything’s impossible until somebody does it.” – Batman

Offline

 

#6 2019-07-01 05:44:25 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6064

Re: how to allow more than one dropped item to be deleted

It looks like you haven't made the earlier change:

Applescript:

on performDragOperation:(info)
set pb to info's draggingPasteboard()
set theURLs to pb's readObjectsForClasses:{current application's NSURL} options:(missing value)
performSelector_withObject_afterDelay_("doFinderStuff:", theURLs, 0.1)
return true
end performDragOperation_


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#7 2019-07-01 05:49:42 pm

Budgie
Member
From:: New Zealand
Registered: 2005-02-22
Posts: 705

Re: how to allow more than one dropped item to be deleted

yup, your'e right, durr roll, forgot to remove

Applescript:

set theURLs to pb's propertyListForType:"NSFilenamesPboardType"

funny, couldn't see that,  cheers


Cheers
Budgie

“Everything’s impossible until somebody does it.” – Batman

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)