Macos Applescript在OS X Firefox上打开.url文件

Macos Applescript在OS X Firefox上打开.url文件,macos,url,applescript,Macos,Url,Applescript,出于只有开发人员才能理解的原因,Firefox将在Windows上创建并打开.url文件,在OS X上创建并打开.webloc文件,但不允许Windows版本的Firefox打开.webloc文件或OS X版本的Firefox打开.url文件。(.url文件在Safari中打开,但由于不值得在此讨论的原因,这还不够好。)作为我在任何一个系统上使用任何一种文件类型的努力的一部分,我正在编写一个applescript以在OS X Firefox上打开.url文件 on open the_droppi

出于只有开发人员才能理解的原因,Firefox将在Windows上创建并打开.url文件,在OS X上创建并打开.webloc文件,但不允许Windows版本的Firefox打开.webloc文件或OS X版本的Firefox打开.url文件。(.url文件在Safari中打开,但由于不值得在此讨论的原因,这还不够好。)作为我在任何一个系统上使用任何一种文件类型的努力的一部分,我正在编写一个applescript以在OS X Firefox上打开.url文件

on open the_droppings
    set filePath to the_droppings

    set fp to open for access filePath
    set fileContents to read fp
    close access fp

    set secondLine to paragraph 2 of fileContents

    set tid to AppleScript's text item delimiters
    set AppleScript's text item delimiters to "="
    set URLstring to last text item of secondLine
    set AppleScript's text item delimiters to tid

    tell application "Firefox"
        activate
        OpenURL URLstring
    end tell
end open
我原以为这会奏效,但在第三行到最后一行,它说“预期的行尾,等等,但找到了标识符。”这是为什么


EDITsakra下面的答案大部分有效,但在包含“=”的URL上出现中断,例如:

Firefox似乎根本没有AppleScript字典。因此,
tell-app“Firefox”
语句中的术语
OpenURL
被解释为AppleScript标识符,而不是AppleScript命令。行中的两个AppleScript标识符会导致语法错误

作为一种解决方法,您可以将shell命令
open
与标准的AppleScript命令
doshell脚本
结合使用:

on open the_droppings

    set filePath to the_droppings

    set fileContents to read filePath

    set theOffset to offset of "URL=" in fileContents
    set URLstring to text (theOffset + 4) through -1 of fileContents

    do shell script "/usr/bin/open -a Firefox.app " & quoted form of URLstring
end open

Firefox似乎根本没有AppleScript字典。因此,
tell-app“Firefox”
语句中的术语
OpenURL
被解释为AppleScript标识符,而不是AppleScript命令。行中的两个AppleScript标识符会导致语法错误

作为一种解决方法,您可以将shell命令
open
与标准的AppleScript命令
doshell脚本
结合使用:

on open the_droppings

    set filePath to the_droppings

    set fileContents to read filePath

    set theOffset to offset of "URL=" in fileContents
    set URLstring to text (theOffset + 4) through -1 of fileContents

    do shell script "/usr/bin/open -a Firefox.app " & quoted form of URLstring
end open

编辑:在应用程序中下载我的脚本,请单击此处:

这更有效:

on open the_droppings
    set filePath to the_droppings
    set fileContents to read filePath

    set theOffsetA to offset of "URL=" in fileContents
    set theOffsetB to offset of "IDList=" in fileContents
    set URLstring to text (theOffsetA + 4) through (theOffsetB - 3) of fileContents

    do shell script "/usr/bin/open -a Firefox.app " & quoted form of URLstring
end open
它将URL=行读取到下一行(IDList)减去3个步骤(忽略\r\n),并将其发送到firefox。对我来说很有魅力。 然而,我看到过布局怪异的url文件(例如:),我不确定它是否能在那里工作。但是我检查了很多我的url文件,他们没有,所以至少它对我来说很好。如果您在使用此脚本时遇到问题,请告诉我。目前,我将其设置为默认应用程序,以打开URL文件,而不是Safari

为了能够使用applescript文件打开url文件,它需要在plist中设置正确的doctype和标识符,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleAllowMixedLocalizations</key>
    <true/>
    <key>CFBundleDevelopmentRegion</key>
    <string>English</string>
    <key>CFBundleDocumentTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeExtensions</key>
            <array>
                <string>url</string>
            </array>
            <key>CFBundleTypeIconFile</key>
            <string>document.icns</string>
            <key>CFBundleTypeName</key>
            <string>URL File</string>
            <key>CFBundleTypeOSTypes</key>
            <array>
                <string>URL</string>
            </array>
            <key>CFBundleTypeRole</key>
            <string>Viewer</string>
        </dict>
    </array>
    <key>CFBundleExecutable</key>
    <string>droplet</string>
    <key>CFBundleIdentifier</key>
    <string>filehandler.url.mozilla.firefox</string>
    <key>CFBundleIconFile</key>
    <string>droplet</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>FirefoxURLHandler</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleSignature</key>
    <string>dplt</string>
    <key>LSMinimumSystemVersionByArchitecture</key>
    <dict>
        <key>x86_64</key>
        <string>10.6</string>
    </dict>
</dict>
</plist>

CbundleAllowMixedLocalizations
CfBundledDevelopmentRegion
英语
CbundleDocumentTypes
CbundleTypeExtensions
网址
CbundleTypeConfigile
document.icns
CbundleTypeName
URL文件
Cbundletypeostypes
统一资源定位地址
CbundleTypeRole
观众
Cbundlexecutable
液滴
CbundleIdentifier
filehandler.url.mozilla.firefox
循环流化床锅炉
液滴
CbundleInfo字典版本
6
CFBundleName
FirefoxURLHandler
CbundlePackageType
应用
CFBundleSignature
dplt
LSMinimumSystemVersionByArchitecture
x86_64
10.6

编辑:在应用程序中下载我的脚本,请点击此处:

这更有效:

on open the_droppings
    set filePath to the_droppings
    set fileContents to read filePath

    set theOffsetA to offset of "URL=" in fileContents
    set theOffsetB to offset of "IDList=" in fileContents
    set URLstring to text (theOffsetA + 4) through (theOffsetB - 3) of fileContents

    do shell script "/usr/bin/open -a Firefox.app " & quoted form of URLstring
end open
它将URL=行读取到下一行(IDList)减去3个步骤(忽略\r\n),并将其发送到firefox。对我来说很有魅力。 然而,我看到过布局怪异的url文件(例如:),我不确定它是否能在那里工作。但是我检查了很多我的url文件,他们没有,所以至少它对我来说很好。如果您在使用此脚本时遇到问题,请告诉我。目前,我将其设置为默认应用程序,以打开URL文件,而不是Safari

为了能够使用applescript文件打开url文件,它需要在plist中设置正确的doctype和标识符,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleAllowMixedLocalizations</key>
    <true/>
    <key>CFBundleDevelopmentRegion</key>
    <string>English</string>
    <key>CFBundleDocumentTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeExtensions</key>
            <array>
                <string>url</string>
            </array>
            <key>CFBundleTypeIconFile</key>
            <string>document.icns</string>
            <key>CFBundleTypeName</key>
            <string>URL File</string>
            <key>CFBundleTypeOSTypes</key>
            <array>
                <string>URL</string>
            </array>
            <key>CFBundleTypeRole</key>
            <string>Viewer</string>
        </dict>
    </array>
    <key>CFBundleExecutable</key>
    <string>droplet</string>
    <key>CFBundleIdentifier</key>
    <string>filehandler.url.mozilla.firefox</string>
    <key>CFBundleIconFile</key>
    <string>droplet</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>FirefoxURLHandler</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleSignature</key>
    <string>dplt</string>
    <key>LSMinimumSystemVersionByArchitecture</key>
    <dict>
        <key>x86_64</key>
        <string>10.6</string>
    </dict>
</dict>
</plist>

CbundleAllowMixedLocalizations
CfBundledDevelopmentRegion
英语
CbundleDocumentTypes
CbundleTypeExtensions
网址
CbundleTypeConfigile
document.icns
CbundleTypeName
URL文件
Cbundletypeostypes
统一资源定位地址
CbundleTypeRole
观众
Cbundlexecutable
液滴
CbundleIdentifier
filehandler.url.mozilla.firefox
循环流化床锅炉
液滴
CbundleInfo字典版本
6
CFBundleName
FirefoxURLHandler
CbundlePackageType
应用
CFBundleSignature
dplt
LSMinimumSystemVersionByArchitecture
x86_64
10.6

我知道这不是apple脚本,但我这样做是为了在Linux上打开它们,但问题是,如果终端关闭,它会关闭浏览器。我就此提出了另一个问题,但这可能会有所帮助

#!/bin/bash
#bash -c "cat $1 | grep URL | cut -d'=' -f2 | xargs firefox &"
echo OpenWinURL in firefox
echo Closing this window will close your firefox.
echo Try opening firefox before opening a OpenWinURL
#echo Copyright 2021 Aaron Peterson GPL V2 or later
#echo  The ampersand doesn't work after firefox
#echo "$1"
#
jobs -l
cat "$1" | grep  -m 1 URL= | cut -d'=' -f2- | xargs -0 -i firefox {} #&
jobs -l
disown -h -a
jobs -l
#read  -n 1 -p "Input Selection:" mainmenuinput

#echo testing
#wait 1000

#
#| head -n 1
#| cut -d'=' -f2 | xargs firefox &

我知道这不是apple脚本,但我这样做是为了在Linux上打开它们,但问题是,如果终端关闭,它会杀死浏览器。我就此提出了另一个问题,但这可能会有所帮助

#!/bin/bash
#bash -c "cat $1 | grep URL | cut -d'=' -f2 | xargs firefox &"
echo OpenWinURL in firefox
echo Closing this window will close your firefox.
echo Try opening firefox before opening a OpenWinURL
#echo Copyright 2021 Aaron Peterson GPL V2 or later
#echo  The ampersand doesn't work after firefox
#echo "$1"
#
jobs -l
cat "$1" | grep  -m 1 URL= | cut -d'=' -f2- | xargs -0 -i firefox {} #&
jobs -l
disown -h -a
jobs -l
#read  -n 1 -p "Input Selection:" mainmenuinput

#echo testing
#wait 1000

#
#| head -n 1
#| cut -d'=' -f2 | xargs firefox &

查看如何在OS X中打开Windows.url文件和在Windows中打开OS X.webloc文件:查看如何在OS X中打开Windows.url文件和在Windows中打开OS X.webloc文件:不幸的是,此脚本对我不起作用,因为它在Firefox中输入的url中包含url文件的IDList部分。可能是Windows Vista或7中引入的内容,但这就是我制作新版本脚本的原因。不幸的是,此脚本对我不起作用,因为它将url文件的IDList部分与Firefox中输入的url一起使用。也许是WindowsVista或Windows7中引入的东西,但这就是我制作新版本脚本的原因。