MacIcon Help please

Hello,

I have been using the “MacIcon Classes” of macoslib, (the latest version, June 6), to display icons of a folderitem in a dropdown menu.

It works in XOJO 2019R1 but not in any version newer than that.
I am using macOS Catalina version 10.15.2 and I am getting these 4 errors…

FileManager.GetFSRefFromFolderItem, line 16
Type “FolderItem” has no member named “MacFSRef”
theFSRef = new FSRef(f.MacFSRef)

FileManager.GetFSRefFromFolderItem, line 16
There is more than one method with this name but this does not match any of the available signatures.
theFSRef = new FSRef(f.MacFSRef)

MacIcon.Draw, line 18
This item does not exist
grafPort = g.Handle(Graphics.handleTypeCGrafPtr)

MacIcon.Draw, line 18
There is more than one method with this name but this does not match any of the available signatures.
grafPort = g.Handle(Graphics.handleTypeCGrafPtr)

Can someone please help.

Thanks.

Lennox

[quote=“Lennox_Jacob1, post:1, topic:55571”]
MacFSRef[/quote]
FolderItem.MacFSRef is deprecated…

Hi Emile,

Is there a workaround or fix?

Thanks.

Lennox

Please move to NSWorkspace and the IconFile function there.
e.g. via MBS Xojo Plugins with NSWorkspaceMBS class or folderItem.IconMBS function.

I do not know, I only searched for FolderItem.MacFSRef in the docs but I do not readed the entry.

It still works for my registered version.

Check the LR.

See Get picture of current application icon for the needed declares.
No expensive Plugin needed at all.

Thanks Christian, Emile, Thomas.

Hi Thomas, or anyone else,

I am testing using Run, I cannot Build, on XOJO 2019r3.2

I would need some help here, this is beyond my capability, below is the code I am using, could you advise how to modify it to be able to use your recommendation?

// Make the base item, this is what will be doing the popping up

dim base as new MenuItem

dim mItem as MenuItem

Dim i, j as integer

j = 0

Dim TheDesktop as FolderItem = SpecialFolder.Desktop

If TheDesktop.Count < 1 then

  msgbox "There are no items on the Desktop"

else

  Dim item As folderitem

  Dim TheIcon as MacIcon

  Dim TheDesktopFilesTrueItemIcon as Picture

  for i = 1 to TheDesktop.Count

    item = TheDesktop.TrueItem(i)

    TheIcon = MacIcon.NewIconFromFolderItem(item)

    TheDesktopFilesTrueItemIcon = TheIcon

    TheIcon.size = 18

    If Trim(TheDesktop.TrueItem(i).DisplayName) = "" or TheDesktop.TrueItem(i).DisplayName.Left(2) = "• " or Trim(TheDesktop.TrueItem(i).DisplayName) = ".DS_Store" then

      'Ignore these items

    else

      mItem = New MenuItem

      mItem.Text = TheDesktop.TrueItem(i).DisplayName

      mItem.Icon = TheIcon  // TheDesktopFileTrueItemIcon

      base.Append(mItem)

      j = j + 1

    end if

  next i

end if

'msgbox str(j)

If j = 0 then

  msgbox "There are no items on the Desktop."

  Return True

else

End if

// Add a Separator

base.Append(new MenuItem(MenuItem.TextSeparator))

// Now display the menu

dim hitItem as MenuItem

hitItem = base.PopUp

if hitItem <> nil then

  msgbox hitItem.Text

End if

Return True

Lennox

Hi @Lennox_Jacob,

although you should really move your code to more modern calls, the following should help you restore MacIcon functionalities.

  1. If there is no NSClassFromString function in your project, create the following method in a module with global scope:
Public Function NSClassFromString(aClassName as CFStringRef) as Ptr
Declare Function NSClassFromString Lib "AppKit.framework" ( aClassName as CFStringRef ) As Ptr
    
    Return  NSClassFromString( aClassName )
End Function
  1. In FileManager, replace the code for GetFSRefFromFolderItem with the following:
declare function URLWithString lib CocoaLib selector "URLWithString:" ( id as Ptr, URLString as CFStringRef ) as Ptr
declare function CFURLGetFSRef lib CarbonLib (CFURLRef as Ptr, ref as Ptr) as Boolean

dim url as Ptr = URLWithString( NSClassFromString( "NSURL" ), f.URLPath )
dim FSRef_ as new MemoryBlock( 80 )

if CFURLGetFSRef( url, FSRef_ ) then
  return new FSRef( FSRef_ )
else
  return  nil
end if

I am not entirely sure it is going to work on Catalina but it works on Mojave.

1 Like

Thanks Stéphane_Mons,

Just to update you, I did that and I am still getting this error…

// MacIcon Classes/Draw

This item does not exist
grafPort = g.Handle(Graphics.handleTypeCGrafPtr)

There is more than one method with this name but this does not match any of the available signatures.
grafPort = g.Handle(Graphics.handleTypeCGrafPtr)

This is on Catalina, and I understand that you are not sure it would work on Catalina.

Thanks again Stéphane_Mons.

Lennox

OK, Graphics.handleTypeCGrafPtr value is 2, but I really doubt it’s gonna work.

I will try to find another way

How do I implement that?

Thanks again.
Lennox

You can search for “Graphics.handleTypeCGrafPtr” and replace every occurrence with “2” and see what happens

Just to be clear, Apple deprecated this method in 10.9, so it could disappear any day now.

https://developer.apple.com/documentation/corefoundation/1584387-cfurlgetfsref

1 Like

Thanks Stéphane_Mons,

Works great!!

Thanks again.
Lennox

Thanks Greg,
Lennox