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
Best Answer
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.