-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathMFF.m
197 lines (165 loc) · 7.06 KB
/
MFF.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//
// MFF.m
// MultiFirefox
//
// Created by David Martorana on 4/7/08.
// Copyright 2008. All rights reserved.
//
#import "MFF.h"
#import <unistd.h>
// Some path constants, for now
static const NSString* __FIREFOX_PROFILE_PATH__ = @"~/Library/Application Support/Firefox/Profiles";
static const NSString* __APPLICATIONS_PATH__ = @"/Applications";
@implementation MFF
// Open the profiles window
+ (void)openFirefoxProfilesWindow:(NSString *)version
{
NSLog(@"version: %@", version);
NSString *toBeCalled = [[@"open -n \"" stringByAppendingString:[self getFirefoxPath:version]] stringByAppendingString: @"\" --args --profilemanager"];
NSLog(@"Profile Launch call: %@", toBeCalled);
system([toBeCalled UTF8String]);
}
+ (NSString *) getFirefoxPath:(NSString *)version
{
NSString *firefoxPath = [__APPLICATIONS_PATH__ stringByAppendingPathComponent:[version stringByAppendingString:@".app"]];
return firefoxPath;
}
// Check to be sure there aren't multiple profiles
+ (BOOL) multipleProfilesExist
{
// Create the appropriate folder name
NSString *folderName = [__FIREFOX_PROFILE_PATH__ stringByExpandingTildeInPath];
NSArray *folderContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folderName error:nil];
// Get the number of directories
unsigned long count = [folderContents count];
if ([folderContents containsObject:@".DS_Store"])
count--;
if ([folderContents containsObject:@".Trashes"])
count--;
NSLog(@"final count is: %lu\n", count);
if (count <= 1)
return NO;
else
return YES;
}
// Get the list of possible profiles
+ (NSArray *) profilesList
{
NSString *profilesFile = [[[__FIREFOX_PROFILE_PATH__ stringByExpandingTildeInPath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"profiles.ini"];
NSLog(@"Attempted path is %@", profilesFile);
if ([[NSFileManager defaultManager] fileExistsAtPath:profilesFile])
{
// Get the contents of the file by line
//NSArray *values = [[[[NSString alloc] initWithContentsOfFile:profilesFile] componentsSeparatedByString:@"\n"] autorelease];
NSString* fileContents = [[[NSString alloc] initWithContentsOfFile:profilesFile encoding:NSUTF8StringEncoding error:nil] autorelease];
NSArray *values = [fileContents componentsSeparatedByString:@"\n"];
NSEnumerator *valuesEnum = [values objectEnumerator];
// Find the profile names
NSString *entry;
NSMutableArray *profileNames = [NSMutableArray arrayWithCapacity:1];
while (entry = [valuesEnum nextObject])
{
NSRange range = [entry rangeOfString:@"Name="];
if (range.location != NSNotFound)
{
NSString *profileName = [entry substringFromIndex:(range.location + range.length)];
if ([profileName isEqualToString:@"default"])
[profileNames insertObject:profileName atIndex:0];
else
[profileNames addObject:profileName];
}
}
// Populate our return array
return [NSArray arrayWithArray:profileNames];
}
// Return our array
return [NSArray array];
}
// Get the list of Firefox versions
+ (NSArray *) versionsList
{
NSDirectoryEnumerator *dirEnum = [[NSFileManager defaultManager] enumeratorAtPath:(NSString*)__APPLICATIONS_PATH__];
NSMutableArray *versionsTemp = [NSMutableArray arrayWithCapacity:0];
NSString *curFileFolderName;
NSString *lowerFileFolderName;
NSString *extension;
BOOL isApp;
BOOL isFirefox;
while (curFileFolderName = [dirEnum nextObject])
{
lowerFileFolderName = [curFileFolderName lowercaseString];
extension = [curFileFolderName pathExtension];
isApp = [extension isEqualToString:@"app"];
isFirefox = ([lowerFileFolderName rangeOfString:@"firefox"].location == 0 ||
[lowerFileFolderName rangeOfString:@"minefield"].location == 0);
if (isFirefox)
{
if (isApp)
{
[versionsTemp addObject:[curFileFolderName substringToIndex:[lowerFileFolderName rangeOfString:@".app"].location]];
[dirEnum skipDescendents];
}
// Don't call skipDescendents here so we can recurse into any non-app Firefox directories
}
else
{
[dirEnum skipDescendents];
}
}
// Put the mutable values into a non-mutable form
return [NSArray arrayWithArray:versionsTemp];
}
// Launch Firefox with the selected profile
+ (void) launchFirefox:(NSString *)version withProfile:(NSString *)profile
{
// Construct the command using 'open'
NSArray *cmdParts = [NSArray arrayWithObjects:@"open -na \"",
[__APPLICATIONS_PATH__ stringByAppendingPathComponent:version],
@".app\" --args -no-remote -P \"",
profile,
@"\"",
nil];
// Send the shell command
system([[cmdParts componentsJoinedByString:@""] UTF8String]);
// Exit this application
exit(0);
}
+ (void) createApplicationWithVersion:(NSString *)version andProfile:(NSString *)profile
{
NSDictionary* errorDict;
NSAppleEventDescriptor* returnDescriptor = NULL;
NSString *firefoxPath = [[[@"" stringByAppendingString:(NSString*)__APPLICATIONS_PATH__] stringByAppendingPathComponent:[version stringByAppendingString:@".app"]] stringByAppendingPathComponent:@"Contents/MacOS/firefox-bin"];
NSString *appName = [[[@"" stringByAppendingString:version] stringByAppendingString:@"-"] stringByAppendingString:profile];
NSString *scriptSource = [NSString stringWithFormat:@"tell application \"AppleScript Editor\"\n\
set myCommand to \"do shell script \\\"%@ -p %@ &> /dev/null &\\\"\"\n\
set contents of document 1 to myCommand\n\
set username to system attribute \"USER\"\n\
compile document 1\n\
set theResult to save document 1 as \"application\" in \"/Users/\" & username & \"/Desktop/%@.app\"\n\
quit\n\
end tell", firefoxPath, profile, appName];
NSAppleScript* scriptObject = [[NSAppleScript alloc] initWithSource:scriptSource];
returnDescriptor = [scriptObject executeAndReturnError: &errorDict];
[scriptObject release];
if (returnDescriptor != NULL)
{
// successful execution
if (kAENullEvent != [returnDescriptor descriptorType])
{
// script returned an AppleScript result
if (cAEList == [returnDescriptor descriptorType])
{
// result is a list of other descriptors
}
else
{
// coerce the result to the appropriate ObjC type
}
}
}
else
{
// no script result, handle error here
}
}
@end