Blame


1 afea5fc3 2010-10-28 rsc #include <u.h>
2 afea5fc3 2010-10-28 rsc #include <stdio.h>
3 afea5fc3 2010-10-28 rsc #include <Carbon/Carbon.h>
4 afea5fc3 2010-10-28 rsc
5 afea5fc3 2010-10-28 rsc AUTOFRAMEWORK(Carbon)
6 afea5fc3 2010-10-28 rsc
7 afea5fc3 2010-10-28 rsc static OSErr Handler(const AppleEvent *event, AppleEvent *reply, long handlerRefcon);
8 afea5fc3 2010-10-28 rsc
9 afea5fc3 2010-10-28 rsc int
10 afea5fc3 2010-10-28 rsc main(void)
11 afea5fc3 2010-10-28 rsc {
12 afea5fc3 2010-10-28 rsc AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, Handler, 0, false);
13 afea5fc3 2010-10-28 rsc RunApplicationEventLoop();
14 afea5fc3 2010-10-28 rsc return 0;
15 afea5fc3 2010-10-28 rsc }
16 afea5fc3 2010-10-28 rsc
17 afea5fc3 2010-10-28 rsc static OSErr
18 afea5fc3 2010-10-28 rsc GetFullPathname(FSSpec *fss, char *path, int len)
19 afea5fc3 2010-10-28 rsc {
20 afea5fc3 2010-10-28 rsc FSRef fsr;
21 afea5fc3 2010-10-28 rsc OSErr err;
22 afea5fc3 2010-10-28 rsc
23 afea5fc3 2010-10-28 rsc *path = '\0';
24 afea5fc3 2010-10-28 rsc err = FSpMakeFSRef(fss, &fsr);
25 afea5fc3 2010-10-28 rsc if (err == fnfErr) {
26 afea5fc3 2010-10-28 rsc /* FSSpecs can point to non-existing files, fsrefs can't. */
27 afea5fc3 2010-10-28 rsc FSSpec fss2;
28 afea5fc3 2010-10-28 rsc int tocopy;
29 afea5fc3 2010-10-28 rsc
30 afea5fc3 2010-10-28 rsc err = FSMakeFSSpec(fss->vRefNum, fss->parID,
31 afea5fc3 2010-10-28 rsc (unsigned char*)"", &fss2);
32 afea5fc3 2010-10-28 rsc if (err)
33 afea5fc3 2010-10-28 rsc return err;
34 afea5fc3 2010-10-28 rsc err = FSpMakeFSRef(&fss2, &fsr);
35 afea5fc3 2010-10-28 rsc if (err)
36 afea5fc3 2010-10-28 rsc return err;
37 afea5fc3 2010-10-28 rsc err = (OSErr)FSRefMakePath(&fsr, (unsigned char*)path, len-1);
38 afea5fc3 2010-10-28 rsc if (err)
39 afea5fc3 2010-10-28 rsc return err;
40 afea5fc3 2010-10-28 rsc /* This part is not 100% safe: we append the filename part, but
41 afea5fc3 2010-10-28 rsc ** I'm not sure that we don't run afoul of the various 8bit
42 afea5fc3 2010-10-28 rsc ** encodings here. Will have to look this up at some point...
43 afea5fc3 2010-10-28 rsc */
44 afea5fc3 2010-10-28 rsc strcat(path, "/");
45 afea5fc3 2010-10-28 rsc tocopy = fss->name[0];
46 afea5fc3 2010-10-28 rsc if ((strlen(path) + tocopy) >= len)
47 afea5fc3 2010-10-28 rsc tocopy = len - strlen(path) - 1;
48 afea5fc3 2010-10-28 rsc if (tocopy > 0)
49 afea5fc3 2010-10-28 rsc strncat(path, (char*)fss->name+1, tocopy);
50 afea5fc3 2010-10-28 rsc }
51 afea5fc3 2010-10-28 rsc else {
52 afea5fc3 2010-10-28 rsc if (err)
53 afea5fc3 2010-10-28 rsc return err;
54 afea5fc3 2010-10-28 rsc err = (OSErr)FSRefMakePath(&fsr, (unsigned char*)path, len);
55 afea5fc3 2010-10-28 rsc if (err)
56 afea5fc3 2010-10-28 rsc return err;
57 afea5fc3 2010-10-28 rsc }
58 afea5fc3 2010-10-28 rsc return 0;
59 afea5fc3 2010-10-28 rsc }
60 afea5fc3 2010-10-28 rsc
61 afea5fc3 2010-10-28 rsc static void
62 afea5fc3 2010-10-28 rsc chk(int err)
63 afea5fc3 2010-10-28 rsc {
64 afea5fc3 2010-10-28 rsc if(err != 0) {
65 afea5fc3 2010-10-28 rsc printf("err %d\n", err);
66 afea5fc3 2010-10-28 rsc exit(1);
67 afea5fc3 2010-10-28 rsc }
68 afea5fc3 2010-10-28 rsc }
69 afea5fc3 2010-10-28 rsc
70 afea5fc3 2010-10-28 rsc static OSErr
71 afea5fc3 2010-10-28 rsc Handler(const AppleEvent *event, AppleEvent *reply, long handlerRefcon)
72 afea5fc3 2010-10-28 rsc {
73 afea5fc3 2010-10-28 rsc AEDesc list;
74 afea5fc3 2010-10-28 rsc DescType type;
75 afea5fc3 2010-10-28 rsc FSSpec f;
76 afea5fc3 2010-10-28 rsc AEKeyword keyword;
77 afea5fc3 2010-10-28 rsc Size actual;
78 afea5fc3 2010-10-28 rsc long len;
79 afea5fc3 2010-10-28 rsc char s[1000];
80 afea5fc3 2010-10-28 rsc
81 afea5fc3 2010-10-28 rsc chk(AEGetParamDesc(event, keyDirectObject, typeAEList, &list));
82 afea5fc3 2010-10-28 rsc chk(AECountItems(&list, &len));
83 afea5fc3 2010-10-28 rsc chk(AEGetNthPtr(&list, 1, typeFSS, &keyword, &type, (Ptr*)&f, sizeof(FSSpec), &actual));
84 afea5fc3 2010-10-28 rsc chk(GetFullPathname(&f, s, sizeof s));
85 afea5fc3 2010-10-28 rsc printf("%s\n", s);
86 afea5fc3 2010-10-28 rsc fflush(stdout);
87 afea5fc3 2010-10-28 rsc
88 afea5fc3 2010-10-28 rsc // uncomment to keep handling more open events
89 afea5fc3 2010-10-28 rsc exit(0);
90 afea5fc3 2010-10-28 rsc }