所以我有这个代码返回当前的safari选项卡的URL
int main(int argc, const char * argv[]) {
NSAppleScript *script= [[NSAppleScript alloc] initWithSource:@"tell application \"Safari\" to return URL of front document as string"];
NSDictionary *scriptError = nil;
NSAppleEventDescriptor *descriptor = [script executeAndReturnError:&scriptError];
if(scriptError) {
NSLog(@"Error: %@",scriptError);
} else {
NSAppleEventDescriptor *unicode = [descriptor coerceToDescriptorType:typeUnicodeText];
NSData *data = [unicode data];
NSString *result = [[NSString alloc] initWithCharacters:(unichar*)[data bytes] length:[data length] / sizeof(unichar)];
NSLog(@"Result: %@",result);
}
return 0;
}
如何实现循环切换所有标签,以便输出所有标签的网址?
答案 0 :(得分:0)
此AppleScript代码返回所有标签的网址
tell application "Safari" to return URL of tabs of window 1
结果是一个列表描述符,必须转换为NSArray
对象
int main(int argc, const char * argv[]) {
NSAppleScript *script = [[NSAppleScript alloc] initWithSource:@"tell application \"Safari\" to return URL of tabs of window 1"];
NSDictionary *scriptError = nil;
NSAppleEventDescriptor *descriptor = [script executeAndReturnError:&scriptError];
if(scriptError) {
NSLog(@"Error: %@",scriptError);
} else {
NSAppleEventDescriptor *listDescriptor = [descriptor coerceToDescriptorType:typeAEList];
NSMutableArray *result = [[NSMutableArray alloc] init];
for (NSInteger i = 1; i <= [listDescriptor numberOfItems]; ++i) {
NSAppleEventDescriptor *URLDescriptor = [listDescriptor descriptorAtIndex:i];
[result addObject: URLDescriptor.stringValue];
}
NSLog(@"Result: %@", [result copy]);
}
return 0;
}
答案 1 :(得分:0)
也许不是你想要的,但这里是Scripting Bridge的解决方案:
SafariApplication *SafariApp = [SBApplication applicationWithBundleIdentifier:@"com.apple.Safari"];
for (SafariWindow *window in SafariApp.windows)
{
for (SafariTab *tab in window.tabs)
NSLog(@"%@", tab.URL);
}