static OSErr PrintAppWhichOpensFile (FSRef *fileRef) { char appPath[2048]; FSRef appRef; LSRolesMask roleMask; OSErr err = noErr; //make sure it's a file, not a folder if (IsFolder(fileRef)) { fprintf(stderr, "Designated path is a folder\n"); return TRUE; } //get the application which opens this item err = LSGetApplicationForItem (fileRef, roleMask, &appRef,NULL); if (err == kLSApplicationNotFoundErr) { printf("This file has no preferred application set.\n", err); return 0; } if (err != noErr) { fprintf(stderr, "An error of type %d occurred in LSGetApplicationForItem()\n", err); return err; } //get path from file reference FSRefMakePath(&appRef, (char *)&appPath, 2048); if (err != noErr) { fprintf(stderr, "An error of type %d occurred in FSRefMakePath()\n", err); return err; } //print out path to application which will open file printf("%s\n", appPath); return noErr; }
static PyObject *Launch_LSGetApplicationForItem(PyObject *_self, PyObject *_args) { PyObject *_res = NULL; OSStatus _err; FSRef inItemRef; LSRolesMask inRoleMask; FSRef outAppRef; CFURLRef outAppURL; if (!PyArg_ParseTuple(_args, "O&l", PyMac_GetFSRef, &inItemRef, &inRoleMask)) return NULL; _err = LSGetApplicationForItem(&inItemRef, inRoleMask, &outAppRef, &outAppURL); if (_err != noErr) return PyMac_Error(_err); _res = Py_BuildValue("O&O&", PyMac_BuildFSRef, &outAppRef, CFURLRefObj_New, outAppURL); return _res; }
void pybase::OpenEditor() { if(!module) return; const char *mname = PyModule_GetFilename(module); if(!mname) { PyErr_Clear(); return; } char fname[1024]; strcpy(fname,mname); // replacing .pyc or .pyo for source file name char *dt = strrchr(fname,'.'); if(dt && !strncmp(dt,".py",2)) strcpy(dt,".py"); // this should open the editor.... #if FLEXT_OS == FLEXT_OS_WIN int err = (int)ShellExecute(NULL,"edit",fname,NULL,NULL,SW_SHOW); if(err == SE_ERR_NOASSOC) { // no association found - try notepad err = (int)ShellExecute(NULL,NULL,"notepad.exe",fname,NULL,SW_SHOW); } else if(err == ERROR_FILE_NOT_FOUND || err == SE_ERR_FNF) post("py/pyext - File not %s found",fname); else if(err <= 32) post("py/pyext - Unknown error opening %s",fname); #elif FLEXT_OS == FLEXT_OS_MAC FSRef ref; OSStatus err = FSPathMakeRef((unsigned char *)fname,&ref,NULL); if(err) post("py/pyext - Error interpreting path %s",fname); else { FSRef editor; err = LSGetApplicationForItem(&ref,kLSRolesEditor,&editor,NULL); if(err) { // Can't find associated application... try Textedit err = FSPathMakeRef((unsigned char *)"/Applications/TextEdit.app",&editor,NULL); if(err) post("py/pyext - Can't find Textedit application"); } if(!err) { LSLaunchFSRefSpec lspec; lspec.appRef = &editor; lspec.numDocs = 1; lspec.itemRefs = &ref; lspec.passThruParams = NULL; lspec.launchFlags = kLSLaunchDefaults; lspec.asyncRefCon = NULL; err = LSOpenFromRefSpec(&lspec,NULL); if(err) post("py/pyext - Couldn't launch editor"); } } #else // thanks to Tim Blechmann char *editor = getenv("EDITOR"); if(!editor) { // || !strcmp(editor, "/usr/bin/nano") || !strcmp(editor, "/usr/bin/pico") || !strcmp(editor, "/usr/bin/vi")) { // no environment variable or console text editor found ... use idle instead (should have come with Python) editor = "idle"; } pid_t child = fork(); if(!child) { char cmd[80]; strcpy(cmd,editor); strcat(cmd," "); strcat(cmd,fname); execl("/bin/sh", "sh", "-c", cmd, (char *) NULL); } #endif }