Create new (.url) file with url address as variable input

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


Solution 1:

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}