1 /////////////////////////////////////////////////////////////////////////////
2 // Name: mac/corefoundation/utilsexc_base.cpp
3 // Purpose: wxMacLaunch
8 // Copyright: (c) Ryan Norton
9 // Licence: wxWindows licence
10 // Notes: Source was originally in utilsexc_cf.cpp,1.6 then moved
11 // to totally unrelated hid.cpp,1.8.
12 /////////////////////////////////////////////////////////////////////////////
14 //===========================================================================
16 //===========================================================================
18 //---------------------------------------------------------------------------
19 // Pre-compiled header stuff
20 //---------------------------------------------------------------------------
22 // For compilers that support precompilation, includes "wx.h".
23 #include "wx/wxprec.h"
27 #include "wx/string.h"
35 #include <CoreFoundation/CoreFoundation.h>
36 #include <ApplicationServices/ApplicationServices.h>
39 #include "wx/filename.h"
40 #include "wx/mac/corefoundation/cfstring.h"
43 #define kDefaultPathStyle kCFURLPOSIXPathStyle
45 //===========================================================================
47 //===========================================================================
49 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
53 // argv is the command line split up, with the application path first
54 // flags are the flags from wxExecute
55 // process is the process passed from wxExecute for pipe streams etc.
56 // returns -1 on error for wxEXEC_SYNC and 0 on error for wxEXEC_ASYNC
57 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
58 bool wxMacLaunch(char **argv
)
60 // Obtains the number of arguments for determining the size of
61 // the CFArray used to hold them
63 for(char** argvcopy
= argv
; *argvcopy
!= NULL
; ++argvcopy
)
68 // If there is not a single argument then there is no application
72 wxLogDebug(wxT("wxMacLaunch No file to launch!"));
77 wxString path
= *argv
++;
79 // Create a CFURL for the application path
80 // Created this way because we are opening a bundle which is a directory
82 CFURLCreateWithFileSystemPath(
86 true); //false == not a directory
88 // Check for error from the CFURL
91 wxLogDebug(wxT("wxMacLaunch Can't open path: %s"), path
.c_str());
95 // Create a CFBundle from the CFURL created earlier
96 CFBundleRef cfbApp
= CFBundleCreate(kCFAllocatorDefault
, cfurlApp
);
98 // Check to see if CFBundleCreate returned an error,
99 // and if it did this was an invalid bundle or not a bundle
100 // at all (maybe a simple directory etc.)
103 wxLogDebug(wxT("wxMacLaunch Bad bundle: %s"), path
.c_str());
108 // Get the bundle type and make sure its an 'APPL' bundle
109 // Otherwise we're dealing with something else here...
110 UInt32 dwBundleType
, dwBundleCreator
;
111 CFBundleGetPackageInfo(cfbApp
, &dwBundleType
, &dwBundleCreator
);
112 if(dwBundleType
!= 'APPL')
114 wxLogDebug(wxT("wxMacLaunch Not an APPL bundle: %s"), path
.c_str());
120 // Create a CFArray for dealing with the command line
121 // arguments to the bundle
122 CFMutableArrayRef cfaFiles
= CFArrayCreateMutable(kCFAllocatorDefault
,
123 cfiCount
-1, &kCFTypeArrayCallBacks
);
124 if(!cfaFiles
) //This should never happen
126 wxLogDebug(wxT("wxMacLaunch Could not create CFMutableArray"));
132 // Loop through command line arguments to the bundle,
133 // turn them into CFURLs and then put them in cfaFiles
134 // For use to launch services call
135 for( ; *argv
!= NULL
; ++argv
)
137 // Check for '<' as this will ring true for
138 // CFURLCreateWithString but is generally not considered
139 // typical on mac but is usually passed here from wxExecute
140 if (wxStrcmp(*argv
, wxT("<")) == 0)
144 CFURLRef cfurlCurrentFile
; // CFURL to hold file path
145 wxFileName
argfn(*argv
); // Filename for path
147 if(argfn
.DirExists())
149 // First, try creating as a directory
150 cfurlCurrentFile
= CFURLCreateWithFileSystemPath(
152 wxCFStringRef(*argv
),
154 true); //true == directory
156 else if(argfn
.FileExists())
158 // And if it isn't a directory try creating it
160 cfurlCurrentFile
= CFURLCreateWithFileSystemPath(
162 wxCFStringRef(*argv
),
164 false); //false == regular file
168 // Argument did not refer to
169 // an entry in the local filesystem,
170 // so try creating it through CFURLCreateWithString
171 cfurlCurrentFile
= CFURLCreateWithString(
173 wxCFStringRef(*argv
),
177 // Continue in the loop if the CFURL could not be created
178 if(!cfurlCurrentFile
)
181 wxT("wxMacLaunch Could not create CFURL for argument:%s"),
186 // Add the valid CFURL to the argument array and then
187 // release it as the CFArray adds a ref count to it
192 CFRelease(cfurlCurrentFile
); // array has retained it
195 // Create a LSLaunchURLSpec for use with LSOpenFromURLSpec
196 // Note that there are several flag options (launchFlags) such
197 // as kLSLaunchDontSwitch etc. and maybe we could be more
198 // picky about the flags we choose
199 LSLaunchURLSpec launchspec
;
200 launchspec
.appURL
= cfurlApp
;
201 launchspec
.itemURLs
= cfaFiles
;
202 launchspec
.passThruParams
= NULL
; //AEDesc*
203 launchspec
.launchFlags
= kLSLaunchDefaults
;
204 launchspec
.asyncRefCon
= NULL
;
206 // Finally, call LSOpenFromURL spec with our arguments
207 // 2nd parameter is a pointer to a CFURL that gets
208 // the actual path launched by the function
209 OSStatus status
= LSOpenFromURLSpec(&launchspec
, NULL
);
211 // Cleanup corefoundation references
216 // Check for error from LSOpenFromURLSpec
219 wxLogDebug(wxT("wxMacLaunch LSOpenFromURLSpec Error: %d"),
224 // No error from LSOpenFromURLSpec, so app was launched