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 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
17 #include "wx/string.h"
24 #include <CoreFoundation/CoreFoundation.h>
25 #include <ApplicationServices/ApplicationServices.h>
28 #include "wx/mac/corefoundation/cfstring.h"
30 long wxMacExecute(wxChar
**argv
,
34 const long errorCode
= ((flags
& wxEXEC_SYNC
) ? -1 : 0);
35 const long successCode
= ((flags
& wxEXEC_SYNC
) ? 0 : -1); // fake PID
39 for(wxChar
** argvcopy
= argv
; *argvcopy
!= NULL
; ++argvcopy
)
44 if(cfiCount
== 0) //no file to launch?
46 wxLogDebug(wxT("wxMacExecute No file to launch!"));
50 CFURLRef cfurlApp
= CFURLCreateWithString(
52 wxMacCFStringHolder(*argv
++, wxLocale::GetSystemEncoding()),
56 CFBundleRef cfbApp
= CFBundleCreate(kCFAllocatorDefault
, cfurlApp
);
59 wxLogDebug(wxT("wxMacExecute Bad bundle"));
65 UInt32 dwBundleType
, dwBundleCreator
;
66 CFBundleGetPackageInfo(cfbApp
, &dwBundleType
, &dwBundleCreator
);
68 //Only call wxMacExecute for .app bundles - others could be actual unix programs
69 if(dwBundleType
!= 'APPL')
76 // We have a good bundle - so let's launch it!
79 CFMutableArrayRef cfaFiles
=
80 CFArrayCreateMutable(kCFAllocatorDefault
, cfiCount
- 1, &kCFTypeArrayCallBacks
);
86 for( ; *argv
!= NULL
; ++argv
)
89 wxString sCurrentFile
;
91 if(wxURI(*argv
).IsReference())
92 sCurrentFile
= wxString(wxT("file://")) + *argv
;
96 CFURLRef cfurlCurrentFile
= CFURLCreateWithString(
98 wxMacCFStringHolder(sCurrentFile
, wxLocale::GetSystemEncoding()),
100 wxASSERT(cfurlCurrentFile
);
106 CFRelease(cfurlCurrentFile
); // array has retained it
110 LSLaunchURLSpec launchspec
;
111 launchspec
.appURL
= cfurlApp
;
112 launchspec
.itemURLs
= cfaFiles
;
113 launchspec
.passThruParams
= NULL
; //AEDesc*
114 launchspec
.launchFlags
= kLSLaunchDefaults
| kLSLaunchDontSwitch
; //TODO: Possibly be smarter with flags
115 launchspec
.asyncRefCon
= NULL
;
117 OSStatus status
= LSOpenFromURLSpec(&launchspec
,
118 NULL
); //2nd is CFURLRef* really launched
127 wxLogDebug(wxT("wxMacExecute ERROR: %d"), (int)status
);
130 return successCode
; //success