Where is the "App Bundle" looking for my media folder? Folder found when running app from Qt Creator, "file not found" when using Terminal - macos

I'm having a little trouble making my app bundle work from the terminal or just double clicking it.
This App actually compiles, links and runs perfectly within the Qt Creator IDE. But, if I try to open it from the terminal I get a "media/file.x file not found" error. The App bundle nor the /Contents/MacOS/executable is finding the "media" folder that is supposed to be beside the executable.
In my app I do something like:
openFile("media/file.x");
In Windows and Linux, this file WILL be found if the "media" folder is exactly in the same hierarchical position of the executable (beside it). On the Mac I have discovered it works differently cause Qt Creator builds an "App Bundle" and the actual executable is inside the /Contents/MacOS folder, so I copied the "media" manually there. This worked without any hassle when "playing" my app from the Qt Creator but as mentioned before it doesn't work when running the bundle itself.
So does anyone know where or how can I homogenize the look for this "media" folder so it works on both: Qt Creator and the App bundle?
Lately, I have been using the following command to "install" the folder on the bundle.
mac {
MediaFiles.files = media
MediaFiles.path = Contents/MacOS
QMAKE_BUNDLE_DATA += MediaFiles
}
Thanks for your help.

After looking for a few days I found a couple of posts discussing the relative path problem. I was just not searching with the right words... The answer is displayed in:
Relative Paths Not Working in Xcode C++
http://www.experimentgarden.com/2009/06/how-to-load-resource-from-your.html
Basically it is necessary to add this:
//On the include part
#ifdef __APPLE__
#include "CoreFoundation/CoreFoundation.h"
#endif
// On the main of your app
// This makes relative paths work in C++ in Xcode by changing directory to the Resources folder inside the .app bundle
#ifdef __APPLE__
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle);
char path[PATH_MAX];
if (!CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path, PATH_MAX))
{
// error!
}
CFRelease(resourcesURL);
chdir(path);
std::cout << "Current Path: " << path << std::endl;
#endif
// ----------------------------------------------------------------------------
IMPORTANT:
The chdir, changes the working path of the app, so it is not necessary to change you relative code after that...

Related

Building Xcode Project Using GNUstep Makefile Does Not Open Application Window, buildtool Can't Find English.lproj

I completed the CurrencyConverter example using Xcode 2.5 on Mac OS X Tiger 10.4.11, which I called CurrencyConverter2 since it was my second attempt. Since I've read that GNUstep is compatible with the Mac OS X Tiger version of Cocoa, I wanted to try building this Xcode project in GNUstep, which I am interested in learning.
I was able to install GNUstep on a VM running FreeBSD 12.0. I created a Makefile called GNUmakefile. Here are the contents of GNUmakefile:
include $(GNUSTEP_MAKEFILES)/common.make
APP_NAME = CurrencyConverter2
CurrencyConverter2_OBJC_FILES = main.m ConverterController.m Converter.m
CurrencyConverter2_RESOURCE_FILES = Info.plist English.lproj/MainMenu.nib
include $(GNUSTEP_MAKEFILES)/application.make
When building the program using gmake, it appears that everything was built correctly:
This is gnustep-make 2.7.0. Type 'gmake print-gnustep-make-help' for help.
Running in gnustep-make version 2 strict mode.
Making all for app CurrencyConverter2...
Creating CurrencyConverter2.app/....
gmake[3]: Warning: File 'main.m' has modification time 21869 s in the future
Compiling file main.m ...
Compiling file ConverterController.m ...
Compiling file Converter.m ...
Linking app CurrencyConverter2 ...
gmake[3]: warning: Clock skew detected. Your build may be incomplete.
Creating CurrencyConverter2.app/Resources...
Creating stamp file...
Creating CurrencyConverter2.app/Resources/Info-gnustep.plist...
Creating CurrencyConverter2.app/Resources/CurrencyConverter2.desktop...
Copying resources into the app wrapper...
However, when I try to open CurrencyConverter2.app, the main window does not show up at all.
My hunch is that for some reason my Makefile is not recognizing English.lproj/MainMenu.nib.
I also tried using a tool called buildtool that is able to build Xcode projects using GNUstep, but it complained that it couldn't find English.lproj even though it is in the directory that I ran buildtool.
When converting a Mac app to GNUstep, you need to make a GNUstep-specific Info-plist template that roughly mirrors Info.plist (which is Mac-only).
At build time, the Info-plist template is used to generate two files: Info-gnustep.plist, and the app's .desktop (freedesktop.org desktop entry) file.
Info-gnustep.plist contains Info entries used at loadtime/runtime, such as the name of the principal class, the main-menu nibfile to load, etc.
The app's .desktop file contains Info entries used for registering the app with the desktop-environment/file-browser, such as the app's desktop-menu category, the executable path, supported MIME types, etc. (Note: 'make install' currently doesn't register a GNUstep app with the desktop environment; In order for an installed GNUStep app to appear in the desktop menus, its .desktop file - found in its Resources folder - must be manually registered using the 'desktop-file-install' command-line tool.)
The Info-plist template should be named, "{APP_NAME}Info.plist", and can contain a standard XML-format plist, or a simple text-list format:
{
{KEY1} = {VALUE1};
{KEY2} = {VALUE2};
...
}
For CurrencyConverter2, create a textfile named "CurrencyConverter2Info.plist" in the same directory as the Makefile, with the contents:
{
ApplicationName = CurrencyConverter2;
FreeDesktopCategories = ("Utility", "X-GNUstep");
NSExecutable = "CurrencyConverter2";
NSMainNibFile = "MainMenu.nib";
NSPrincipalClass = NSApplication;
NSRole = Application;
}
GNUstep-make will automatically find CurrencyConverter2Info.plist, so it doesn't need an entry in the Makefile.
Unrelated to the Info-plist template issue, you can also make these changes to your Makefile:
Info.plist is Mac-only, so it can be removed from
CurrencyConverter2_RESOURCE_FILES
MainMenu.nib can be specified as a localized resource (so it no longer needs English.lproj in its pathname) by removing it from CurrencyConverter2_RESOURCE_FILES and adding these two lines to your Makefile:
CurrencyConverter2_LOCALIZED_RESOURCE_FILES = MainMenu.nib
CurrencyConverter2_LANGUAGES = English

Ruby Shoes App packaged for Windows doesn't display images

When I package my Shoes App for Windows images doesn't work. I have this very simple app (min_test.rb):
Shoes.app()
{
background "bg.jpg"
}
Which works fine if I run it through Shoes, but when I package it with Shoes so that I get a standalone .exe file the images doesn't load. Inside the folder these are the contents:
min_test.rb
bg.jpg
min_test.exe (after packaging)
If Shoes3.3.1, indeed, on linux too.
console (alt-/) saying "couldn't find tmp.....bg.jpg" ?
(background or image)
could you file a bug report, please : https://github.com/Shoes3/shoes3/issues
EDIT: Sorry, wait
if Shoes3.3.1 !
you have to first make a shy then an exe and the resulting exe will be outside of the directory where your script and image is.
follow this : https://github.com/Shoes3/shoes3/wiki/Packaging
(note: tested on linux and on Windows)

How to get a .exe packaged Ruby Shoes app to reference resources outside the package?

I'm trying to make a standalone .exe packaged Ruby Shoes app that uses images dynamically, meaning whichever images is found in the folder of the .exe file.
Shoes.app() {
background "bg.jpg"
}
This code works if the image is in the same folder when the .exe is packaged, and the image seems to be packaged into the .exe since it's not needed in the same folder as the .exe for it to display when running the exe. But when you want it to load the file in the same folder as the .exe, packaging the app without the image, it does not show. I've tried different ways at finding absolute path to the current directory where the .exe is launched from, but they all seem to point to some temporary directory under AppData and not where the .exe file is located.
Edit: my first answer was incomplete. Windows is a little odd in Shoes for packaged apps. Write a little test script.
Shoes.app do
stack do
para "DIR: #{DIR}"
para "LIB_DIR: #{LIB_DIR}"
cdir = Dir.getwd
para "CWD: #{cdir}"
end
end
Dir.getwd is probably what you want.
Calling pwd should get you what you want
Nope, Ok sorry get it now !:-)
Shoes is opening your exe/shy into AppData/temp so working directory and __FILE__ both point there !
Someone, some time ago proposed this : Trying to access the "current dir" in a packaged Shoes app
must be a better way !
EDIT:
you probably want custom packager (check "i want advanced install options")
check "Expand shy in users directory"
Do as you done for regular packaging.
Now when launching the exe, it will ask you( or the user) to choose where to install your app, proceed, note the directory.
Now before launching the installed app feed the noted directory with your resources and you should be ok
Some references : https://github.com/Shoes3/shoes3/wiki/Custom-Install-Scripts
(there's a lot more to it)
https://github.com/Shoes3/shoes3/issues/247#issuecomment-213919829

Packaging mp3 files into Qt app

Since I found mp3 files can't be retrieved by QMediaPlayer from resource, I figure it out to use them as local file. I use this cmd with a little modification to copy files into installation directory:
copydata.commands = $(COPY_DIR) $$PWD/resources/sound $$OUT_PWD/HomeControl.app/Contents/MacOS
first.depends = $(first) copydata
export(first.depends)
export(copydata.commands)
QMAKE_EXTRA_TARGETS += first copydata
So those files existed in the /HomeControl.app/Contents/MacOS/sound. From Qt Creator the app plays the mp3 perfectly, but if executed from the build directory it doesn't play the mp3 at all! Really don't know why.
arm away btn clicked
current media: "file:///Dev/Qt_Sample/build-test_widgets-Desktop_Qt_5_3_0_clang_64bit-Debug/HomeControl.app/Contents/MacOS/sound/System_Arm_Away.mp3"
disarm stay btn clicked
current media: "file:///Dev/Qt_Sample/build-test_widgets-Desktop_Qt_5_3_0_clang_64bit-Debug/HomeControl.app/Contents/MacOS/sound/System_Disarmed.mp3"
Is there any difference from executing in the Qt creator & directly from the App? Like debug mode or what?
Or is there other ways to correctly deploy mp3 files into the bundle?
This is how I build in resources for OS X bundles:
mac {
Resources.files = dirInTheProjectDirectory
Resources.path = Contents/MacOS
QMAKE_BUNDLE_DATA += Resources
}
You can call Resources whatever you fancy.
That copies them to Contents/MacOS/dirInTheProjectDirectory. You can then access them with QDir(QCoreApplication::applicationDirPath()+"/dirInTheProjectDirectory/");
The likely problem is when you're accessing them in your code you're pointing to the path in the build directory, and not the path that's within your bundle.
Here's my final solution. Thank you #nicholas-smith.
In the .pro file:
# deploy with mp3 folder
mac {
Resources.files = ./resources/sound
Resources.path = Contents/MacOS
QMAKE_BUNDLE_DATA += Resources
}
Then accessing those file with:
// pass the path of mp3 to the QMediaPlayer
playAudio(QCoreApplication::applicationDirPath()
+ "/sound/System_Disarmed.mp3");
From log it shows:
arm away btn clicked
current media: "file:///Dev/Qt_Sample/build-test_widgets-Desktop_Qt_5_3_0_clang_64bit-Debug/HomeControl.app/Contents/MacOS/sound/System_Arm_Away.mp3"

Open CV, image loading issue on Mac OS X

I think my question is pretty basic. I was trying to get Open CV to install on my OSX Lion. I had followed all the steps recommended on this link http://tilomitra.com/opencv-on-mac-osx/
However, when I run the C++ code recommended on the website in Xcode, it fails to load an image with the cvLoadImage( ) function. I have placed my image in the project folder (as recommended). Here is the code I was running:
// Example showing how to read and write images
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv/cvaux.hpp>
int main(int argc, char** argv)
{
IplImage * pInpImg = 0;
// Load an image from file - change this based on your image name
pInpImg = cvLoadImage("my_image.jpg", CV_LOAD_IMAGE_UNCHANGED);
if(!pInpImg)
{
fprintf(stderr, "failed to load input image\n");
return -1;
}
// Write the image to a file with a different name,
// using a different image format -- .png instead of .jpg
if( !cvSaveImage("my_image_copy.png", pInpImg) )
{
fprintf(stderr, "failed to write image file\n");
}
// Remember to free image memory after using it!
cvReleaseImage(&pInpImg);
return 0;
}
So during execution, the code builds successfully, but always ends up in the following loop and halts execution:
if(!pInpImg)
{
fprintf(stderr, "failed to load input image\n");
return -1;
}
Has anyone faced such a problem before? How could I solve this?
(During installation of 'Macports' and 'Cmake', I had received an alert saying that Xcode was not installed or was installed without 'Command Line Tools'. But as per another thread on this forum, I had installed these from the Xcode-->Preferences-->Downloads folder on installing Xcode.
However, still during installation, 'Macports' and 'Cmake' gave me warnings, but installed anyway. But could this be the issue? )
Thank you!
The problem is that you're putting the image into the project folder instead of the folder containing your executable. You can either put the image file in the folder with the executable or put the full path to the image in the call to cvLoadImage.
Older versions of Xcode put the executable in either the build/Debug or build/Release folder in the project folder. Newer versions of Xcode put the build products in the project folder in the DerivedData folder. You can find the DerivedData folder by going to File -> Project Settingsā€¦ and clicking the arrow next to the folder path:
In the project folders, go to 'Products' > right click on the executable file > 'Show in Finder' > put the input image there. The output image ('.png' file) will go here as well.

Resources