1 /////////////////////////////////////////////////////////////////////////////
2 // Name: mac/corefoundation/utilsexc_base.cpp
3 // Purpose: wxMacExecute
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 long wxMacExecute(wxChar
**argv
,
60 wxProcess
*WXUNUSED(process
))
62 // Semi-macros used for return value of wxMacExecute
63 const long errorCode
= ((flags
& wxEXEC_SYNC
) ? -1 : 0);
64 const long successCode
= ((flags
& wxEXEC_SYNC
) ? 0 : -1); // fake PID
66 // Obtains the number of arguments for determining the size of
67 // the CFArray used to hold them
69 for(wxChar
** argvcopy
= argv
; *argvcopy
!= NULL
; ++argvcopy
)
74 // If there is not a single argument then there is no application
78 wxLogDebug(wxT("wxMacExecute No file to launch!"));
83 wxString path
= *argv
++;
85 // Create a CFURL for the application path
86 // Created this way because we are opening a bundle which is a directory
88 CFURLCreateWithFileSystemPath(
92 true); //false == not a directory
94 // Check for error from the CFURL
97 wxLogDebug(wxT("wxMacExecute Can't open path: %s"), path
.c_str());
101 // Create a CFBundle from the CFURL created earlier
102 CFBundleRef cfbApp
= CFBundleCreate(kCFAllocatorDefault
, cfurlApp
);
104 // Check to see if CFBundleCreate returned an error,
105 // and if it did this was an invalid bundle or not a bundle
106 // at all (maybe a simple directory etc.)
109 wxLogDebug(wxT("wxMacExecute Bad bundle: %s"), path
.c_str());
114 // Get the bundle type and make sure its an 'APPL' bundle
115 // Otherwise we're dealing with something else here...
116 UInt32 dwBundleType
, dwBundleCreator
;
117 CFBundleGetPackageInfo(cfbApp
, &dwBundleType
, &dwBundleCreator
);
118 if(dwBundleType
!= 'APPL')
120 wxLogDebug(wxT("wxMacExecute Not an APPL bundle: %s"), path
.c_str());
126 // Create a CFArray for dealing with the command line
127 // arguments to the bundle
128 CFMutableArrayRef cfaFiles
= CFArrayCreateMutable(kCFAllocatorDefault
,
129 cfiCount
-1, &kCFTypeArrayCallBacks
);
130 if(!cfaFiles
) //This should never happen
132 wxLogDebug(wxT("wxMacExecute Could not create CFMutableArray"));
138 // Loop through command line arguments to the bundle,
139 // turn them into CFURLs and then put them in cfaFiles
140 // For use to launch services call
141 for( ; *argv
!= NULL
; ++argv
)
143 // Check for '<' as this will ring true for
144 // CFURLCreateWithString but is generally not considered
145 // typical on mac but is usually passed here from wxExecute
146 if (wxStrcmp(*argv
, wxT("<")) == 0)
150 CFURLRef cfurlCurrentFile
; // CFURL to hold file path
151 wxFileName
argfn(*argv
); // Filename for path
153 if(argfn
.DirExists())
155 // First, try creating as a directory
156 cfurlCurrentFile
= CFURLCreateWithFileSystemPath(
158 wxCFStringRef(*argv
),
160 true); //true == directory
162 else if(argfn
.FileExists())
164 // And if it isn't a directory try creating it
166 cfurlCurrentFile
= CFURLCreateWithFileSystemPath(
168 wxCFStringRef(*argv
),
170 false); //false == regular file
174 // Argument did not refer to
175 // an entry in the local filesystem,
176 // so try creating it through CFURLCreateWithString
177 cfurlCurrentFile
= CFURLCreateWithString(
179 wxCFStringRef(*argv
),
183 // Continue in the loop if the CFURL could not be created
184 if(!cfurlCurrentFile
)
187 wxT("wxMacExecute Could not create CFURL for argument:%s"),
192 // Add the valid CFURL to the argument array and then
193 // release it as the CFArray adds a ref count to it
198 CFRelease(cfurlCurrentFile
); // array has retained it
201 // Create a LSLaunchURLSpec for use with LSOpenFromURLSpec
202 // Note that there are several flag options (launchFlags) such
203 // as kLSLaunchDontSwitch etc. and maybe we could be more
204 // picky about the flags we choose
205 LSLaunchURLSpec launchspec
;
206 launchspec
.appURL
= cfurlApp
;
207 launchspec
.itemURLs
= cfaFiles
;
208 launchspec
.passThruParams
= NULL
; //AEDesc*
209 launchspec
.launchFlags
= kLSLaunchDefaults
;
210 launchspec
.asyncRefCon
= NULL
;
212 // Finally, call LSOpenFromURL spec with our arguments
213 // 2nd parameter is a pointer to a CFURL that gets
214 // the actual path launched by the function
215 OSStatus status
= LSOpenFromURLSpec(&launchspec
, NULL
);
217 // Cleanup corefoundation references
222 // Check for error from LSOpenFromURLSpec
225 wxLogDebug(wxT("wxMacExecute LSOpenFromURLSpec Error: %d"),
230 // No error from LSOpenFromURLSpec, so app was launched