0

I am looking for a way to create a new .url file in the current folder, but have an input field that allows me to enter the desired webaddress the file should link to. The whole process should be similar as to how windows handles the creation of a new shortcut, meaning it should as for the link address and then create the shortcut in the active folder.

Is there any way to realize this with Apple Script and / or Keyboard Maestro? This would be useful as I often find myself wanting to add .url files to folders.

Thanks! A2k

EDIT:

So I managed to get the above somewhat working with the code below, but still face a couple of problems: * the filename is hardcoded as "Shortcut.url". The script breaks though if the filename "shortcut.url" already exists * the script breaks when pressing the "Cancel" button of the file dialog window. How can I address this scenario and add error handling?

tell application "Finder"
    try
        display dialog "Enter URL / IP Address below:" default answer ""
        set urlName to the text returned of result
        if length of urlName = 0 then
            return 0
        end if
        set fileExt to ".url"
        set thisFolder to the target of the front window as alias
        set newFile to "Shortcut" & fileExt

        make new file at thisFolder with properties {name:newFile, file type:"TEXT", creator type:"ttxt"}

        set tFile to open for access (thisFolder as text) & newFile with write permission
        try
            write ("
[InternetShortcut]
URL=" & urlName & "
IconIndex=0
            ") to tFile
            close access tFile
        on error
            close access tFile
        end try


    on error errMsg
        display dialog (errMsg)
    end try
end tell

Appreciate any help, A2k

2

I wrote this thinking that you were looking to create the the Mac Version of a .URL file, which would be a .webloc file... But after reading your code, it seems my assumption was wrong. I put too much effort into my solution to just scrap it, so in the event that you need to automate the creation of .webloc files, in the future... Here you go

This AppleScript code works for me using the latest version of macOS Mojave.

set theURL to text returned of (display dialog "Enter Desired URL" default answer ¬
    "https://stackexchange.com" buttons {"Cancel", "OK"} ¬
    default button "OK" cancel button "Cancel" with title "Enter Desired URL")

try
    tell application "Finder" to tell its window 1
        set theFolder to POSIX path of (target as alias)
    end tell
on error errMsg number errNum
    set theFolder to POSIX path of (choose folder)
end try

tell application "Finder"
    activate
    set theName to text returned of (display dialog "Enter Desired Name" default answer ¬
        "File Name" buttons {"Cancel", "OK"} ¬
        default button "OK" cancel button "Cancel" with title "Enter Desired Webloc File Name")
    set fileExists to exists of alias ((theFolder & theName) as POSIX file as text)
    repeat while fileExists is true
        activate
        set theName to text returned of (display dialog "A File With That Name Already Exixts. Please Choose A Different Name" default answer ¬
            "File Name" buttons {"Cancel", "OK"} ¬
            default button "OK" cancel button "Cancel" with title "Enter Desired Webloc File Name")
        set fileExists to exists of alias ((theFolder & theName) as POSIX file as text)
    end repeat
end tell

tell application "Finder" to make new internet location file at POSIX file theFolder to theURL with properties {name:theName}
| improve this answer | |
  • @user3439894 . I edited the code and implemented solutions to your comments. Please let me know if you spot any pitfalls with the new version. – wch1zpink Apr 15 '19 at 22:02
  • @Armitage2k It's already set up for that And if you have no finder windows open, A dialog will pop-up asking you to choose a folder – wch1zpink Apr 16 '19 at 1:07
  • I have never heard of .webloc before this post, so thank you :) I prefer .url files because the shortcut also needs to be opened on PC, hence not sure if webloc is supported on windows machines. – Armitage2k Apr 16 '19 at 1:08
  • @wch1zpink oh I see, I just ran it without finder window open, hence got confused. Great answer, thank you very much. – Armitage2k Apr 16 '19 at 1:09
  • Nice update to the code! – user3439894 Jun 1 '19 at 1:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .