代码之家  ›  专栏  ›  技术社区  ›  Dinah SLaks

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

  •  1
  • Dinah SLaks  · 技术社区  · 16 年前

    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
    

    我原以为这会奏效,但在第三行到最后一行,它说“预期的行尾,等等,但找到了标识符。”这是为什么?

    编辑 sakra在下面的回答大部分是有效的,但在包含“=”的URL上出现了中断,例如: http://example.com?foo=a&bar=z

    2 回复  |  直到 16 年前
        1
  •  2
  •   Dinah SLaks    16 年前

    Firefox似乎根本没有AppleScript字典。因此,这个术语 OpenURL 在 tell app "Firefox"

    作为解决方法,您可以使用shell命令 open 与标准的AppleScript命令结合使用 do shell script

    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
    
        2
  •  0
  •   laarsk    14 年前

    编辑:在应用程序中下载我的脚本,请单击此处: http://www.mediafire.com/?v77bv9gl9e7oj40

    这更有效:

    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文件(例如: http://forums.mozillazine.org/viewtopic.php?p=2619487 ),我不确定它在那里是否有效。但是我检查了很多我的url文件,他们没有,所以至少它对我来说很好。如果您在使用此脚本时遇到问题,请告诉我。目前,我将其设置为默认应用程序,以打开URL文件,而不是Safari!

    <?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>
    
        3
  •  -1
  •   Aaron Peterson    5 年前

    #!/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 &
    
    推荐文章