I’m trying to explore ASOC a little bit more everyday and was now trying to replace the old applescript way:
tell application "Finder" to move finderPathOfCurrentItemToTrash to the trash
with a more ASOC way of doing it. I have this code:
set theWorkspace to current application's NSWorkspace's sharedWorkspace()
set theNewURLsDictionary to current application's NSDictionary's dictionary()
set theErrorObject to current application's NSError's errorWithDomain_code_userInfo_((current application's NSPOSIXErrorDomain), 1, missing value)
set theMoveToTrashResults to theWorkspace's recycleURLs_completionHandler_({currentEntryPathURL}, theNewURLsDictionary, theErrorObject)
but i get a crash with no errors. Anyone has an idea as to what I may be doing wrong? Right now I am passing a one item array composed of an NSURL object initialized like this:
set currentEntryPathURL to current application's |NSURL|'s fileURLWithPath_isDirectory_(currentEntryPath, true)
I tried passing an NSString before but didn’t seem to work.
Am i pushing beyond the limits of ASOC??
Thanks for any help.
Browser: Safari 531.22.7
Operating System: Mac OS X (10.6)
Yeah, after a bit more of reading I found this in the docs:
If I am not mistaken, blocks are a no-no in ASOC, right?
But maybe this would work:
arguments-wise it’s all good except for the last one, tag. In the docs it says:
how would I specify a variable to pass as an argument? Initialize an NSInteger object perhaps? And how would I read this variable back? Maybe like this?
set theMoveToTrashResults to theWorkspace's performFileOperation_source_destination_files_tag_(current application's NSWorkspaceRecycleOperation, {currentEntryPathURLParentFolder}, missing value, {currentEntryPathURL}, NSIntegerObject)
and this destination argument gets me puzzled. What is the URL for the trash? I passed missing value here but maybe it’ll fail…
Browser: Safari 531.22.7
Operating System: Mac OS X (10.6)
I don’t think that will help. You were right about blocks and ASOC – you can’t do blocks, and if the method you are using is working, it’s only because you are allowed to pass a nil for the block if you don’t care about the results. You’ll have to use ObjC if you want to see the results of the operation, and if there were any errors.
As you said, blocks are out, so recycleURLs_completionHandler_ will only work with missing value as the second argument. There’s no way you can get feedback without blocks here, although I suppose you could check the old path and be pretty safe in assuming that if it’s not there, it’s worked. The problem, though, is that the command is asynchronous, so you won’t know quite when to check.
performFileOperation:source:destination:files:tag: should be OK. The problem with the tag argument is that it’s a pointer to a C type, not to an object. There is a workaround for this, but it’s not intuitive. Have a look at the Reference chapter ‘What Methods Can Return’. So something like this (untested):
set {theMoveToTrashResults,theTag} to theWorkspace's performFileOperation_source_destination_files_tag_(current application's NSWorkspaceRecycleOperation, currentEntryPathURLParentFolder, "", {"File 1","File 2"}, reference)
Interesting… i’ll look that up for sure. But one final question: is there a constant or something that could give me the correct URL to the trash? Looked in the docs, saw nothing that could help… Then, where is it located really? I assume it is a hidden folder that starts with a dot somewhere in the user’s folder… has anyone seen the trash?
Thanks everyone! This has been very instructive post for me.
Good night
Browser: Safari 531.22.7
Operating System: Mac OS X (10.6)
Obvious to the point where it pokes your eyes… I was thinking more in the Obj-C world, but that’ll do. Seems to simply be “~/.Trash”, which makes it universal. Excellent. Thanks!
Browser: Safari 531.22.7
Operating System: Mac OS X (10.6)