从第三个应用程序获取浏览器URL

时间:2018-11-28 08:26:16

标签: c++ macos url applescript

我正在使用MacOS应用程序,我想知道是否有一种方法可以从中获取活动浏览器的URL。应用程序是用C ++完成的。

我想在不使用AppleScript的情况下获得它。

有可能吗?

谢谢

1 个答案:

答案 0 :(得分:1)

这不是一个简单的答案,但好消息是“是的,无需使用AppleScript就可以做到这一点”,而坏消息是“您必须首先使用AppleScript”。

让我详细说明一下:浏览器应用程序通常具有Applescript词典(您可以使用/Applications/Utilities文件夹中的“脚本编辑器”应用程序进行查看。这是Google Chrome浏览器的词典外观:{ {3}} 您会看到我已经找到“ tab”类,并且在其中可以看到URL属性。

因此,您需要做的是先完成AppleScript ,以获取要定位的浏览器的窗口。然后,当它起作用时,您需要将AppleScript转换为基础的原始AppleEvents(AppleScripts编译为原始内容)。 AppleScripts和AppleEvents都可以用代码完成。

您真正正在寻找的答案(例如“我可以使用一些最高机密的API还是将代码中的URL发送到本地计算机上的某个开放端口中以查询浏览器?”)据我所知不存在。 AppleScript和AppleEvents是Apple提供的用于自动执行大多数应用程序的长期方式,您需要利用它们。

如果您决定继续使用AppleScript并可能将其转换为AppleEvents,这就是我要做的。

1)查找公开可用的AppleScript来完成所需的工作。

2)将AppleScript转换为AppleEvents

3)编写AppleEvents的代码。

对于1,这是一个遍历Safari中每个窗口的脚本,该脚本是我从Script Editor Dictionary for Google Chrome获得的:

tell application "Safari"

--Variables
set windowCount to number of windows
set docText to ""

--Repeat for Every Window
repeat with x from 1 to windowCount
set tabCount to number of tabs in window x

--Repeat for Every Tab in Current Window
repeat with y from 1 to tabCount
--Get Tab Name & URL
set tabName to name of tab y of window x
set tabURL to URL of tab y of window x
end repeat

end repeat
end tell

对于步骤2,我认为您可以使用this tutorial查看原始事件和产生的结果。

对于第3步,我用C语言编写的古老代码将获得给定窗口ID的浏览器窗口的URL。

OSStatus CreateTheAppleEventForGetURL( OSType appSignature, long windowid, char **retval) 
{
    OSErr               err = noErr;
    AEAddressDesc       targetAddress;
    AEDescList          replyDesc = { typeNull, nil };
    AEKeyword           keyword;
    DescType            desiredClass;   
    AEDesc              replySingle, theOptionalAttributeDesc, theSeldDesc,theObjSpec,theThirdObjSpec,theSecondObjSpec,theFormDesc,theNullDesc;
    AppleEvent          theEvent, reply;
    AEIdleUPP           theIdleProc;
    Boolean             gotReply = false;
    long                errNumber=0;
    long                buffer;
    Size                actualSize;
    char                *result = NULL;

    theIdleProc = NewAEIdleUPP((AEIdleProcPtr)&TheIdleFunction );
    if( NULL != theIdleProc )
    {
        err = AECreateDesc( typeApplSignature, &appSignature, sizeof( appSignature ), &targetAddress );

        if( noErr == err )
        {
            err = AECreateAppleEvent( 'core', 'getd', &targetAddress, kAutoGenerateReturnID, kAnyTransactionID, &theEvent );
            buffer = 0x10000;
            err = AECreateDesc('magn', &buffer, 4, &theOptionalAttributeDesc);
            if( noErr == err )
            {
                err = AECreateDesc(typeNull, nil, 0, &theNullDesc);
                desiredClass = 'cwin';
                buffer = 'ID  ';
                err = AECreateDesc('enum',&buffer,4,&theFormDesc);
                buffer = windowid;
                err = AECreateDesc(typeLongInteger,&buffer,4,&theSeldDesc);
                err = CreateObjSpecifier(desiredClass,&theNullDesc,'ID  ',&theSeldDesc,true,&theThirdObjSpec);
                buffer = 'docu';
                err = AECreateDesc(typeType,&buffer,4,&theSeldDesc);
                desiredClass = 'prop';
                err = CreateObjSpecifier(desiredClass,&theThirdObjSpec,'prop',&theSeldDesc,true,&theSecondObjSpec);
                buffer = 'prop';
                err = AECreateDesc('enum',&buffer,4,&theFormDesc);
                err = AECreateDesc(typeNull, nil, 0, &theObjSpec);
                buffer = 'pURL';
                err = AECreateDesc(typeType,&buffer,4,&theSeldDesc);
                err = CreateObjSpecifier(desiredClass,&theSecondObjSpec,'prop',&theSeldDesc,true,&theObjSpec);      
                err = AEPutAttributeDesc(&theEvent,'csig',&theOptionalAttributeDesc);
                err = AEPutParamDesc(&theEvent,keyDirectObject, &theObjSpec);
            }
            if( noErr == err )
            {
                err = AESend( &theEvent, &reply, kAEWaitReply + kAENeverInteract, kAENormalPriority, 120, theIdleProc, NULL );
                if( noErr == err )
                {
                    gotReply = true;
                }
                else
                {
                    gotReply = false;                   
                }
                err = AEDisposeDesc(&theObjSpec);
                err = AEDisposeDesc(&theOptionalAttributeDesc);
                err = AEDisposeDesc(&theSeldDesc);
                err = AEDisposeDesc(&theSecondObjSpec);
                err = AEDisposeDesc(&theThirdObjSpec);
                err = AEDisposeDesc(&theFormDesc);
                err = AEDisposeDesc(&theNullDesc);              
            }
        }
        err = AEGetParamPtr(&reply, keyErrorNumber, typeLongInteger, NULL, &errNumber, sizeof(errNumber), &actualSize);
        if(true == gotReply )
        {
            err = AEGetParamDesc( &reply, keyDirectObject, typeAEList, &replyDesc );

            keyword = typeAEList;
            err = AEGetNthDesc( &replyDesc, 1, typeUnicodeText, &keyword, &replySingle);

            if( noErr == err)
            {
                OSStatus            status;
                Size                theSize;
                UnicodeMapping      iUnicodeMapping;
                UnicodeToTextInfo   theInfo;
                UniChar             theName[512];
                unsigned char       crapola[512]; // a.k.a. a pstring

                iUnicodeMapping.unicodeEncoding = kTextEncodingUnicodeDefault;
                iUnicodeMapping.otherEncoding = kTextEncodingMacRoman;
                iUnicodeMapping.mappingVersion = kUnicodeUseLatestMapping;
                status = CreateUnicodeToTextInfo(&iUnicodeMapping,&theInfo);                
                theSize = AEGetDescDataSize(&replySingle);

                err = AEGetDescData(&replySingle,&theName,512);
                if( noErr == err)
                {
                    err = ConvertFromUnicodeToPString(theInfo,theSize,theName,crapola);
                    if( noErr == err )
                    {
                        result = malloc( theSize * sizeof( char ));
                        if( NULL != result )
                        {
                            p2cstrcpy(result,crapola);
                            printf( "URL returned is %s\n", result);
                        }
                    }
                }
                status = DisposeUnicodeToTextInfo(&theInfo);
            }
        }
        err = AEDisposeDesc( &targetAddress );
        err = AEDisposeDesc( &replySingle );
        DisposeAEIdleUPP( theIdleProc );
    }
    if( NULL != retval )
        *retval = result;
    return(err);
}

我敢肯定the Accessory View Pane in Script Editor自macOS 10.8起不会编译,但是您明白了。

希望这篇长篇文章对您有所帮助!