File creation - Current directory problem

Mac OSX specific forum
Lebostein
Addict
Addict
Posts: 849
Joined: Fri Jun 11, 2004 7:07 am

File creation - Current directory problem

Post by Lebostein »

Hello,

this code generates two small files:

Code: Select all

CreatePreferences("x.ini")
PreferenceGroup("test")
WritePreferenceString("name", "PB")
ClosePreferences()

CreateFile(0, "x.txt")
WriteString(0, "Hello")
CloseFile(0)
If I compile and run this code with IDE, the files in the same directory created as the source code. If I create an executable in the same directory an run this app, then I found the files in root directory of my system. What is wrong?
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: File creation - Current directory problem

Post by ts-soft »

you have to use a path like: GetPathPart(ProgramFilename()) or SetCurrentDirectory(GetPathPart(ProgramFilename()))
This is the same on all OS to make sure you use the right path.
Lebostein
Addict
Addict
Posts: 849
Joined: Fri Jun 11, 2004 7:07 am

Re: File creation - Current directory problem

Post by Lebostein »

OK. In directory

/Users/Lebostein/DATEN/PureBasic/TomyTex

I have two files

> Setting.ini
> TomyTex.pb


If I run TomyTex.pb with IDE, the program can open the ini file with simple OpenPreferences("Setting.ini") without problems. If I check the execution path with ProgramFilename() I get

/tmp/PureBasic0.app/Contents/MacOS/PureBasic0

Why my program finds the ini file? The execution path is an other!

However: If I generate an app an run this app, I get

/Users/Lebostein/DATEN/PureBasic/TomyTex/TomyTex.app/Contents/MacOS/TomyTex

with ProgramFilename(). The right path this time, but the program don't find the ini file...
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: File creation - Current directory problem

Post by ts-soft »

Set the compileroptions to create exe in sourcedir! or you have self to difference exe and compile from IDE. the same problem on all OS.

greetings
thomas
Lebostein
Addict
Addict
Posts: 849
Joined: Fri Jun 11, 2004 7:07 am

Re: File creation - Current directory problem

Post by Lebostein »

ts-soft wrote:the same problem on all OS.
No!

With windows does not care if I

1) run source with IDE and exe creation in temp dir
2) run source with IDE and exe creation in source dir
3) run compiled exe in source dir

In all cases the program can open the ini file in source directory with simple OpenPreferences("Setings.ini"). In all cases the current directory = source directory.

With Mac OS, only 1) and 2) works (Your hint with the compiler options is not necessary). With 3), the current directory = root directory.

Is this a Mac specific problem/peculiarity, that a started app select the root directory as current directory (then I will find a workaround for Mac version of my program)? Or is this a problem of apps generated with PB only?
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: File creation - Current directory problem

Post by ts-soft »

if you start a program from windows-shell (cmd), you have the same problem, currentdirectory is not set. only the most filemanager sets this on windows, but is not sure, the user start it with a filemanager. if you start from ide, the ide set the currentdirectory!
in your finish application you should always use the path from ProgramFileName(), on all OS!
(better not put settings in the same dir as program!)

greetings
Thomas
Lebostein
Addict
Addict
Posts: 849
Joined: Fri Jun 11, 2004 7:07 am

Re: File creation - Current directory problem

Post by Lebostein »

Hm... you could have right.

But I have not seen a PB source code in my life (not in forum, not in example folder and not in code archiv), in which the current path was set...
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: File creation - Current directory problem

Post by ts-soft »

Lebostein wrote:Hm... you could have right.

But I have not seen a PB source code in my life (not in forum, not in example folder and not in code archiv), in which the current path was set...
i have posted a hint on this more than 10x in german-board :wink:
i use it in my applications!
Lebostein
Addict
Addict
Posts: 849
Joined: Fri Jun 11, 2004 7:07 am

Re: File creation - Current directory problem

Post by Lebostein »

OK.

Fred should change the behaviour of ProgramFilename() on Mac. Now I get the full path to the binary:

/Users/Lebostein/PureBasic/Application/Application.app/Contents/MacOS/Application

What we need for a platform independet behaviour is

/Users/Lebostein/PureBasic/Application/Application.app

Workaround (not save):

Code: Select all

exe.s = ProgramFilename()
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS: exe = Left(exe, FindString(exe, "/Contents/MacOS/", 1) - 1): CompilerEndIf
SetCurrentDirectory(GetPathPart(exe))
User avatar
michel51
Enthusiast
Enthusiast
Posts: 290
Joined: Mon Nov 21, 2005 10:21 pm
Location: Germany

Re: File creation - Current directory problem

Post by michel51 »

Lebostein wrote:OK. In directory

/Users/Lebostein/DATEN/PureBasic/TomyTex

I have two files

> Setting.ini
> TomyTex.pb


If I run TomyTex.pb with IDE, the program can open the ini file with simple OpenPreferences("Setting.ini") without problems. If I check the execution path with ProgramFilename() I get

/tmp/PureBasic0.app/Contents/MacOS/PureBasic0

Why my program finds the ini file? The execution path is an other!

However: If I generate an app an run this app, I get

/Users/Lebostein/DATEN/PureBasic/TomyTex/TomyTex.app/Contents/MacOS/TomyTex

with ProgramFilename(). The right path this time, but the program don't find the ini file...
That's right, because the ini-file is not within the application-folder.
Try this: a right click on the compiled file (TomyTex.app) will open a menu list. There click on 'show contents' ("Paketinhalte zeigen"). In this folder open the the folder "contents". Copy your ini-file here in. Or into the MacOs folder.

Additional files are not automatically copied into the contents folder while compiling to .app :!:

Hope, it helps.
michel51

Mac OS X Snow Leopard (10.6.8 ) Intel
PureBasic V 5.21(x64), V 5.22beta
WilliamL
Addict
Addict
Posts: 1255
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: File creation - Current directory problem

Post by WilliamL »

Hey michel51, how's it going?

You're right the files are in the Contents folder and if you have to update your files you can make a tool. I made a tool to re-save my (modified) plist over the default that is created every time the app is created.

@Lebostein

If you are having problems finding your files when you run from the IDE and from the app then you can define your path with an If/EndIf using the #PB_EditorCreateExecutable constant value. (the path will be different) There is a thread about how to do this ( http://www.purebasic.fr/english/viewtop ... 41&start=0 ) but I seem to remember that it didn't work quite right for me so I have my own routine.
MacBook Pro-M1 (2021), Tahoe 26.1, PB 6.30b2
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: File creation - Current directory problem

Post by PB »

@Lebostein: Why put yourself through all this hassle and pain?
Make your code open the files using GetPathPart() for your app
as ts-soft said, and be done with it. You're just making artificial
problems for yourself at the moment.

And never use SetCurrentDirectory() because from what I've read
in the past, it can actually affect other apps. So they say.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
jamirokwai
Enthusiast
Enthusiast
Posts: 799
Joined: Tue May 20, 2008 2:12 am
Location: Cologne, Germany
Contact:

Re: File creation - Current directory problem

Post by jamirokwai »

WilliamL wrote:Hey michel51, how's it going?

You're right the files are in the Contents folder and if you have to update your files you can make a tool. I made a tool to re-save my (modified) plist over the default that is created every time the app is created.
Hi WilliamL,

are you willing to share this tools code??? :-)
Regards,
JamiroKwai
WilliamL
Addict
Addict
Posts: 1255
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: File creation - Current directory problem

Post by WilliamL »

@pb - :?:

@jamirokwai
If you are asking me how to make a tool then I have to say that I do not. I asked in this thread http://www.purebasic.fr/english/viewtop ... hilit=tool how to automatically save my modified plist over the default plist when I created an executable. I had made a little program, with preferences, a window, and an event loop that copied the plist and/or resources to the newly created app. freak responded that I could just make the code a tool (in the IDE). As you can tell from my post I don't quite understand why it works but it appears that if you have code that does something, in this case copy one file over another one, you can make it a tool.
MacBook Pro-M1 (2021), Tahoe 26.1, PB 6.30b2
Post Reply