Mac + FolderItem differences

I’m trying to port my project from Windows to Mac (latest Mojave version), latest 2019 1.1 Xojo.

I have some code here to load some info from a text file:

[code]#If DebugBuild
f = GetFolderItem("").Parent.Child(“streets.txt”)
#Else
f = GetFolderItem(“streets.txt”)
#EndIf

Dim t As textinputstream = textinputstream.open(f)[/code]

This loads from the text file regardless if its a debug build or the actual compiled product. This fails on a Mac with a IOException 2 error, but works fine with Windows 10. The textinputstream line is the one that fails.

The text file i’m loading is in the same folder as the Xojo main project file and is in the project itself too. How would I change this to work on a Mac?

I recommend the FindFile function I use in a lot of MBS examples to find files in the same place as the app:

[code]Function FindFile(name as string) As FolderItem
// Look for file in parent folders from executable on

		dim parent as FolderItem = app.ExecutableFile.Parent
		
		while parent<>Nil
			
			dim file as FolderItem = parent.Child(name)
			
			if file<>Nil and file.Exists then
				Return file
			end if
			
			parent = parent.Parent
		wend
	End Function[/code]

Ok, thanks. How would I use this find my file ‘streets.txt’ for example? Would using this solve my issue on Mac?

It would be better still to NOT store your datafiles “next” to the app, and store them in the folders specifically designated for them

Fair enough, but how do I make it work either way? It works perfectly in Windows, as I said, so its a Mac file access issue I can assume.

put them in the correct place “APP Support?” and it works “either” way

[code]
f = FindFile(“streets.txt”)

Dim t As textinputstream = textinputstream.open(f)[/code]

That was my idea. Independent of platform look for the file next to the app.

As I don’t know what kind of app it is, I wouldn’t judge on having files next to the app.

What you can do is use a CopyFiles step to copy the streets.txt file into your built application’s resources folder. With that, use SpecialFolder.Resources to access the file.

You can’t commercially distribute a Mac app that tries to access files next to the app, Apple’s security standards don’t make it easy.

One note about Tims response… files in SPECIAL.RESOURCES cannot/should not be written to, only read

leave that out… Xojo adds the method header and footer for you automatically

Dave…yep figured that out after a few. :slight_smile:

It works! Thanks, Christian!

[quote=454316:@Derek DiBenedetto]This loads from the text file regardless if its a debug build or the actual compiled product. This fails on a Mac with a IOException 2 error, but works fine with Windows 10. The textinputstream line is the one that fails.

The text file i’m loading is in the same folder as the Xojo main project file and is in the project itself too. How would I change this to work on a Mac?[/quote]
Don’t do this for the Mac. Apple have guidelines on specific locations that executable code and non-executable code can be stored in. While your application will work when breaching these guidelines, Apple’s code signing API some times throws hissy fits when files in the wrong place, which prevents you from releasing your application on the internet or via the Mac App Store.

This causes all kinds of grief as you only find out when you try to release your application; and at that point you realize you need to redesign your app if you want to ship it on the Mac.

Non-executable files should be stored in the “Resources” folder, and to create confusion, what Apple means in this context is non Mach-O binary files, so even script files should go in the “Resources” folder.

I figured as much. My app won’t be on the App Store (it’ll be sold elsewhere), so I won’t have to worry about this much. But Windows 10 is sort of the same way with non .exe writable files needing to be elsewhere too (AppData usually).

I apologize if I wasn’t clear; code signing is a process that you must complete in order to publically distribute your app. If you don’t code sign, most people will not be able to open your app. Regardless of where you sell it.

Yes, I see that. What exactly are the minimum steps required for this? I did check out Apple’s site, but it isn’t super clear. Do I need a $99 Dev Account to do this? I don’t plan on submitting the app to the App Store.

Yes, you need a dev account. You only get the certificates if you have the account. App Store or no App Store you need to pay the 99$/year Apple tax.

Check out the forum for threads about “my certificates stopped working”. Everyone goes through that in some form. The explanations are confusing, convoluted and usually outdated. It’s intense fun.

I can recommend Sam’s AppWrapper app, which will code-sign and notarise your app. It did mine the other day and rather to my astonishment the whole process was successful. I was faintly expecting an e-mail back from Apple saying my certs had all been cancelled and self-destruct had been initiated on all my Macs.