[Cocoa] Extra file operations

Mac OSX specific forum
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

[Cocoa] Extra file operations

Post by kenmo »

As long as you Cocoa masters keep giving these great answers, I will keep asking Cocoa questions :D

Today I am wondering how to
(a) move a file to the Trash (instead of a pure delete)
(b) display the "Get Info" window about a file

I have figured out both of these using Windows API (Recycle Bin and Properties) but I am still learning how to work with Cocoa...

Thanks for any tips!
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: [Cocoa] Extra file operations

Post by wilbert »

Moving to trash can be done with the NSWorkspace methods recycleURLs:completionHandler: or performFileOperation:source:destination:files:tag:
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: [Cocoa] Extra file operations

Post by Shardik »

The following code example needs at least Mountain Lion to work. It creates a file and moves it to trash. If a file with the same filename already exists in trash, the file will be renamed to a unique filename before moving it to trash. You can simply prove it by executing this program a second time - the program moved to trash will obtain a modified filename...

I have tested the code example successfully on Mountain Lion and Yoesmite.

The NSWorkspace method recycleURLs:completionHandler: proposed by Wilbert won't work in PureBasic because it utilizes a completion handler block. This is a technique which I still haven't been able to implement in PureBasic. It would be very interesting if Wilbert knows a way... :twisted:

The NSWorkspace method performFileOperation:source:destination:files:tag: would indeed be an alternative that even should work with MacOS versions older than Mountain Lion!

Code: Select all

EnableExplicit

Define Error.I = CocoaMessage(0, 0, "NSError alloc")
Define Result.I
Define TestFile.S = GetTemporaryDirectory() + "Test.Txt"
Define TestFileURL.I
Define TrashFileURL.I

If OSVersion() < #PB_OS_MacOSX_10_8
  MessageRequester("Error",
    "Sorry, but this program needs at least Mountain Lion!")
  End
EndIf

If CreateFile(0, TestFile)
  WriteString(0, "This file should be moved to the trash can!")
  CloseFile(0)

  TestFileURL = CocoaMessage(0, 0,
    "NSURL fileURLWithPath:$", @TestFile,
    "isDirectory:", #NO)
  TrashFileURL = CocoaMessage(0, 0, "NSURL alloc")

  If TestFileURL
    If TrashFileURL
      Result = CocoaMessage(0, CocoaMessage(0, 0, "NSFileManager defaultManager"),
        "trashItemAtURL:", TestFileURL,
        "resultingItemURL:", @TrashFileURL,
        "error:", @Error)

      If Result = #YES
        MessageRequester("Success", "The file '" + GetFilePart(TestFile) +
          "'" + #CR$ + "was successfully moved to trash as file" + #CR$ + "'" +
          PeekS(CocoaMessage(0, CocoaMessage(0, TrashFileURL,
          "lastPathComponent"), "UTF8String"), -1, #PB_UTF8) + "'")
      Else
        MessageRequester("Error", "Moving file " + GetFilePart(TestFile) +
          " to trash has failed!" + #CR$ + #CR$ + PeekS(CocoaMessage(0,
          CocoaMessage(0, Error, "localizedDescription"), "UTF8String"),
          -1, #PB_UTF8))
      EndIf

      CocoaMessage(0, TrashFileURL, "release")
    EndIf

    CocoaMessage(0, TestFileURL, "release")
  EndIf
EndIf
Last edited by Shardik on Thu Jul 16, 2015 9:37 pm, edited 1 time in total.
WilliamL
Addict
Addict
Posts: 1224
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: [Cocoa] Extra file operations

Post by WilliamL »

I like it!

So instead of saving my VIF (very important file) over the old version I can move the old file into the trash and save the new version. In a way it is like a backup and with the auto renaming I can actually have a sequence of old versions.

Is there an extra If/then loop in there?

Code: Select all

  If TestFileURL
    If TrashFileURL
MacBook Pro-M1 (2021), Sonoma 14.4.1, PB 6.10LTS M1
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: [Cocoa] Extra file operations

Post by wilbert »

Shardik wrote:The NSWorkspace method recycleURLs:completionHandler: proposed by Wilbert won't work in PureBasic because it utilizes a completion handler block. This is a technique which I still haven't been able to implement in PureBasic. It would be very interesting if Wilbert knows a way... :twisted:
I posted a little info on blocks in this thread http://www.purebasic.fr/english/viewtop ... 19&t=57219
In this case however, you are allowed to pass 'nil' for the completionHandler which is much more convenient.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: [Cocoa] Extra file operations

Post by Shardik »

WilliamL wrote:Is there an extra If/then loop in there?

Code: Select all

  If TestFileURL
    If TrashFileURL
During testing I separated these If statements because each URL needs a corresponding "release" just before the corresponding EndIf... :wink:

Inititially I coded

Code: Select all

If TestFileURL And TrashFileURL ...
wilbert wrote:I posted a little info on blocks in this thread viewtopic.php?f=19&t=57219
How could I have overlooked this... :oops:

About a year ago I had to stop my programming trials in PureBasic with the GameKit framework in implementing GameCenter queries because with each new OS X release the improved GameKit API methods increased their usage of completion handler blocks. Now I know what experiments I will try the coming weekend... :wink:

Nevertheless as an alternative I have recently made huge progress in controlling the Mac's Game Center app by utilizing the accessibilty API in PureBasic and automatically evaluating all the pinball table highscores where I am on global rank 1 (9 tables until now) and where I have been beaten recently... :lol:
WilliamL
Addict
Addict
Posts: 1224
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: [Cocoa] Extra file operations

Post by WilliamL »

@Shardik, no, you were right. I didn't see the difference between TestFileURL and TrashFileURL. My mistake.

Ok, it works perfectly except after moving the file to the trash, and sucessfully saving the new file, I get back to the 'WaitWindowEvent()' and I get an 'Invalid Memory Access' error. [later] ok, if I take out 'CocoaMessage(0, TrashFileURL, "release")' I don't get the error message. Is this action necessary?

see corrected code "here"
Last edited by WilliamL on Fri Jul 17, 2015 9:30 pm, edited 1 time in total.
MacBook Pro-M1 (2021), Sonoma 14.4.1, PB 6.10LTS M1
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: [Cocoa] Extra file operations

Post by Shardik »

kenmo wrote:(b) display the "Get Info" window about a file
This code example displays the "Get Info" window for a specified file (successfully tested with Snow Leopard and Yosemite):

Code: Select all

EnableExplicit

ImportC ""
  NSPerformService(*ItemName, *NSPasteboard)
EndImport

Define Pasteboard.I
Define TargetFile.S = #PB_Compiler_Home + "logo.png"
Define TypeArray.I

; ----- Create Pasteboard
Pasteboard = CocoaMessage(0, 0, "NSPasteboard pasteboardWithUniqueName")

If Pasteboard
  TypeArray = CocoaMessage(0, 0,
    "NSArray arrayWithObject:$", @"NSStringPboardType")

  If TypeArray
    CocoaMessage(0, Pasteboard,
      "declareTypes:", TypeArray,
      "owner:", 0)

    If CocoaMessage(0, Pasteboard,
      "setString:$", @TargetFile,
      "forType:$", @"NSStringPboardType") = #YES

      ; ----- Show info for selected file
      NSPerformService(CocoaMessage(0, 0,
        "NSString stringWithString:$", @"Finder/Show Info"), Pasteboard)
    EndIf
  EndIf
EndIf
If you also need to open the Finder with the specified file selected you may try the following extended code:

Code: Select all

EnableExplicit

ImportC ""
  NSPerformService(*ItemName, *NSPasteboard)
EndImport

Define Pasteboard.I
Define TargetFile.S = #PB_Compiler_Home + "logo.png"
Define TypeArray.I
Define Workspace.I = CocoaMessage(0, 0, "NSWorkspace sharedWorkspace")

If Workspace
  ; ----- Open Finder and select target file
  CocoaMessage(0, Workspace,
    "selectFile:$", @TargetFile,
    "inFileViewerRootedAtPath:$", @"")

  ; ----- Create Pasteboard
  Pasteboard = CocoaMessage(0, 0, "NSPasteboard pasteboardWithUniqueName")

  If Pasteboard
    TypeArray = CocoaMessage(0, 0,
      "NSArray arrayWithObject:$", @"NSStringPboardType")

    If TypeArray
      CocoaMessage(0, Pasteboard,
        "declareTypes:", TypeArray,
        "owner:", 0)

      If CocoaMessage(0, Pasteboard,
        "setString:$", @TargetFile,
        "forType:$", @"NSStringPboardType") = #YES

        ; ----- Show info for selected file
        NSPerformService(CocoaMessage(0, 0,
          "NSString stringWithString:$", @"Finder/Show Info"), Pasteboard)
      EndIf
    EndIf
  EndIf
EndIf
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: [Cocoa] Extra file operations

Post by Shardik »

WilliamL wrote:Ok, it works perfectly except after moving the file to the trash, and sucessfully saving the new file, I get back to the 'WaitWindowEvent()' and I get an 'Invalid Memory Access' error. [later] ok, if I take out 'CocoaMessage(0, TrashFileURL, "release")' I don't get the error message. Is this action necessary?
William, you are right. Wilbert has sent me a PM with following explanations for mistakes in my code from above (Wilbert, again thank you very much, you are simply the best mentor I can imagine!):
wilbert wrote:A few remarks about your code example ...
Return objects like resultingItemURL and error should not be allocated nor released.
The method itself allocates them and they are autoreleased.
For objects you create yourself, in general those created with alloc or new need to be released manually and other ones are autoreleased.
The object created by NSURL fileURLWithPath therefore is an autorelease object so you should not release it manually otherwise it might result in a crash.
For other readers of this thread I therefore decided against editing my above code example but to post here the modified version with the changes proposed by Wilbert so that others may also learn from my mistakes:

Code: Select all

EnableExplicit

Define Error.I
Define Result.I
Define TestFile.S = GetTemporaryDirectory() + "Test.Txt"
Define TestFileURL.I
Define TrashFileURL.I

If OSVersion() < #PB_OS_MacOSX_10_8
  MessageRequester("Error",
    "Sorry, but this program needs at least Mountain Lion!")
  End
EndIf

If CreateFile(0, TestFile)
  WriteString(0, "This file should be moved to the trash can!")
  CloseFile(0)

  TestFileURL = CocoaMessage(0, 0,
    "NSURL fileURLWithPath:$", @TestFile,
    "isDirectory:", #NO)

  If TestFileURL
    Result = CocoaMessage(0, CocoaMessage(0, 0, "NSFileManager defaultManager"),
      "trashItemAtURL:", TestFileURL,
      "resultingItemURL:", @TrashFileURL,
      "error:", @Error)

    If Result = #YES
      MessageRequester("Success", "The file '" + GetFilePart(TestFile) +
        "'" + #CR$ + "was successfully moved to trash as file" + #CR$ + "'" +
        PeekS(CocoaMessage(0, CocoaMessage(0, TrashFileURL,
        "lastPathComponent"), "UTF8String"), -1, #PB_UTF8) + "'")
    Else
      MessageRequester("Error", "Moving file " + GetFilePart(TestFile) +
        " to trash has failed!" + #CR$ + #CR$ + PeekS(CocoaMessage(0,
        CocoaMessage(0, Error, "localizedDescription"), "UTF8String"),
        -1, #PB_UTF8))
    EndIf
  EndIf
EndIf
WilliamL
Addict
Addict
Posts: 1224
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: [Cocoa] Extra file operations

Post by WilliamL »

Thanks Shardik for the new code it works perfectly.

I appreciate Wilbert's and your efforts to understand Cocoa and it is a great addition to the PB forum. I can't thank you both enough and I'm sure there are many others that feel the same way. :D
Last edited by WilliamL on Fri Jul 17, 2015 9:31 pm, edited 1 time in total.
MacBook Pro-M1 (2021), Sonoma 14.4.1, PB 6.10LTS M1
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: [Cocoa] Extra file operations

Post by kenmo »

Lots of great information here, thank you! I will try it out as soon as possible.
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: [Cocoa] Extra file operations

Post by Shardik »

Here is another code example to move a file to trash that utilizes the NSWorkspace method recycleURLs:completionHandler: proposed by Wilbert. It works with MacOS versions beginning with Snow Leopard and I have tested it successfully on Snow Leopard and Yosemite:

Code: Select all

EnableExplicit

Define FileArray.I
Define TestFile.S = GetTemporaryDirectory() + "Test.Txt"
Define TestFileURL.I
Define Workspace.I = CocoaMessage(0, 0, "NSWorkspace sharedWorkspace")

If OSVersion() < #PB_OS_MacOSX_10_6
  MessageRequester("Error",
    "Sorry, but this program needs at least Snow Leopard!")
  End
EndIf

If Workspace
  If CreateFile(0, TestFile)
    WriteString(0, "This file should be moved to the trash can!")
    CloseFile(0)

    ; ----- Create URL from TestFile string
    TestFileURL = CocoaMessage(0, 0,
      "NSURL fileURLWithPath:$", @TestFile,
      "isDirectory:", #NO)

    If TestFileURL
      ; ----- Create Array of URLs using TestFileURL (only 1 element)
      FileArray = CocoaMessage(0, 0, "NSMutableArray arrayWithCapacity:", 1)
      CocoaMessage(0, FileArray, "addObject:", TestFileURL)

      If FileArray
        ; ----- Move the URLs to the trash in the same manner as in the Finder
        CocoaMessage(0, Workspace,
          "recycleURLs:", FileArray,
          "completionHandler:", 0)
        MessageRequester("Info", "The file '" + TestFile + "'" + #CR$ +
          "was successfully moved to trash!")
      EndIf
    EndIf
  EndIf
EndIf
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: [Cocoa] Extra file operations

Post by kenmo »

Shardik, is it "safe" to package your Get Info code in a procedure like this?

Do any of these objects need to be freed? Is it bad to call these repeatedly (I'm not sure what a "pasteboard" is yet)...

Code: Select all

Procedure GetInfo(File.s)
  Protected Pasteboard.i = CocoaMessage(0, 0, "NSPasteboard pasteboardWithUniqueName")
  If (Pasteboard)
    Protected TypeArray.i = CocoaMessage(0, 0, "NSArray arrayWithObject:$", @"NSStringPboardType")
    If (TypeArray)
      CocoaMessage(0, Pasteboard, "declareTypes:", TypeArray, "owner:", 0)
      If (CocoaMessage(0, Pasteboard, "setString:$", @File, "forType:$", @"NSStringPboardType") = #YES)
        NSPerformService(CocoaMessage(0, 0, "NSString stringWithString:$", @"Finder/Show Info"), Pasteboard)
      EndIf
    EndIf
  EndIf
EndProcedure
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: [Cocoa] Extra file operations

Post by Shardik »

kenmo wrote:Shardik, is it "safe" to package your Get Info code in a procedure like this?

Do any of these objects need to be freed? Is it bad to call these repeatedly (I'm not sure what a "pasteboard" is yet)...
At first you still have to import NSPerformService() for your procedure GetInfo(File.s) to work:

Code: Select all

ImportC ""
  NSPerformService(*ItemName, *NSPasteboard)
EndImport
An NSPasteboard is a clipboard. I assume that PureBasic on MacOS X is wrapping NSPasteboard methods for its native clipboard commands. But PureBasic's clipboard is a public clipboard whereas the Cocoa method pasteboardWithUniqueName will create a unique clipboard which won't interfere with the contents of other clipboards. This unique clipboard will be closed automatically when your program ends, so in order to not create a new unique clipboard with each call of your procedure I would use Static to create this clipboard together with the TypeArray only once in your procedure:

Code: Select all

ImportC ""
  NSPerformService(*ItemName, *NSPasteboard)
EndImport

Procedure GetInfo(File.s)
  Static Pasteboard.i
  Protected TypeArray.i

  If Pasteboard = 0
    Pasteboard = CocoaMessage(0, 0, "NSPasteboard pasteboardWithUniqueName")

    If Pasteboard
      TypeArray.i = CocoaMessage(0, 0,
        "NSArray arrayWithObject:$", @"NSStringPboardType")

      If TypeArray
        CocoaMessage(0, Pasteboard, "declareTypes:", TypeArray, "owner:", 0)
      EndIf
    EndIf
  EndIf

  If CocoaMessage(0, Pasteboard,
    "setString:$", @File,
    "forType:$", @"NSStringPboardType") = #YES
    NSPerformService(CocoaMessage(0, 0,
      "NSString stringWithString:$", @"Finder/Show Info"), Pasteboard)
  EndIf
EndProcedure
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: [Cocoa] Extra file operations

Post by kenmo »

Thanks for the additional information! I will use one static Pasteboard pointer, and I can leave TypeArray as a Protected.
Shardik wrote:At first you still have to import NSPerformService() for your procedure GetInfo(File.s) to work:
Of course. I also like to wrap them in compiler safeguards, so that multiple snippets in the same project can import the same API function without PB compilation errors.

Code: Select all

ImportC ""
  CompilerIf (Not Defined(NSPerformService, #PB_Procedure))
    NSPerformService(*ItemName, *NSPasteboard)
  CompilerEndif
EndImport
Post Reply