Jump to content

Saving .webloc file to user-specified location.


Recommended Posts

I'm trying to create a workflow to save a link (.webloc file) to a user-specified location. I'm new to Alfred and coding, so I thought I would start off "slow" but, as it turns out, it's not as straightforward as I hoped and I've spent an embarrassingly long time trying to figure it out! It's time to ask for help :)


 


I've been piecing together the code via snippets found online and trial and error. Here's what I have so far:


try
     --Get URL and Title of page from Google Chrome
     tell application "Google Chrome" to tell active tab of window 1
          set u to URL
          set t to title
     end tell

     --Set default folder to save in; use the form "set defaultpath to "Macintosh HD:Users:katie:Downloads" as alias" for another specific location
     set defaultpath to (path to downloads folder) as alias

     --Make link and ask user to choose name (title of page is suggested) and location (starts at default folder)
     tell application "Finder"
          make new internet location file to u at {choose file name with prompt "Save file as:" default location defaultpath default name t}
          open information window of result
     end tell
end try

From everything I can find online, this should work. I know that there are problems with it because, from what I understand, it will alert the user if there is already a file with the same name and ask them if they want to replace it or not but then won't actually do those actions. But I thought I would focus on getting a file to actually save first...This code opens a save as dialogue with the pre-populated name and location, but nothing is saved. Sometimes the default save location doesn't work either and it goes instead to my Documents folder.

 

I found two solutions but they are not ideal. The following solution definitely works but doesn't allow for a user-specified name and location:



tell application "Finder"
     make new internet location file at desktop to u with properties {name:t}


I could use this to save to the Downloads folder, then access the file through Alfred to move it to somewhere else. But, it seems like it should be possible to make the process faster. This solution can save a .url to any location:



try
     tell application "Safari"
          set u to URL of (document of window 1)
          set t to name of window 1

          set defaultpath to (path to downloads folder) as alias

          set savedfile to (choose file name with prompt "Save file as:" default location defaultpath default name t)
          set savepath to (POSIX path of savedfile) & ".url"
     end tell

     set res to "[InternetShortcut]URL=" & u

     set cmd to "echo \"" & res & "\" > " & quoted form of savepath
     do shell script cmd
on error x
     activate me
     display dialog x
end try


While this option has the added benefit of making files that can be opened across platforms, Chrome cannot currently handle such files (and they open as plain text). I could get an extension to open them as a workaround but I'd prefer to find a way to get the first script to work. I also don't really understand this code (the shell script part).

 

So, I'm asking for help. Can anyone see why my first Applescript is not actually saving a file?

 

 

Link to post

I haven't gotten to the point of adding it to a workflow - since it doesn't even work in the Applescript Editor. I was planning to do a hot key and keyword linked to a NSAppleScript object. I understand that this would involve wrapping the script in this:

on alfred_script(q)
  -- your script here
end alfred_script
Link to post

Okay a bit confusing with the snippets all doing different tasks. Here is a solution that will

 

- Give you a prompt asking where the webloc file should be saved (it is here that you can move around and save it to a different directory)

- The name of the file to be saved is pre-populated with the name/title of the browser window.

tell application "Safari" to set thename to name of front document

tell application "Safari" to set theUrl to URL of front document
tell application "Finder"
	set resultFile to (choose file name with prompt "Save As File" default name thename default location path to desktop) as text
	--	if resultFile does not end with ".webloc" then set resultFile to resultFile & ".webloc"
	make new internet location file to theUrl with properties {name:thename}
end tell

I need more info and clear info if this does not work. Please clear up if you want to use Safari or Chrome as they have different methods of grabbing browser info, just wording changes but it matters.

 

You do want webloc files correct? I am super confused by the last script where you have it save the url to a .url file prefixed with `[internetShortcut]URL=`

(the shell in that script is doing nothing but taking the url passed to it and adding the [internetShortcut]URL= to it URL)

 

Please let me know if I missed what you needed completely, what is wrong, items to change, ideally could use...  

Edited by twinpeaks
Link to post

Thanks for your reply! I'm sorry for being unclear. Basically, I have been scouring the internet for bits of Applescript that I can piece together. This is what I have so far:

try
     --Get URL and Title of page from Google Chrome
     tell application "Google Chrome" to tell active tab of window 1
          set u to URL
          set t to title
     end tell

     --Set default folder to save in; use the form "set defaultpath to "Macintosh HD:Users:katie:Downloads" as alias" for another specific location
     set defaultpath to (path to downloads folder) as alias

     --Make link and ask user to choose name (title of page is suggested) and location (starts at default folder)
     tell application "Finder"
          make new internet location file to u at {choose file name with prompt "Save file as:" default location defaultpath default name t}
          open information window of result
     end tell
end try

This is what I want the code to do:

  1. Get the URL and title of page of the front most tab of Google Chrome.
  2. Save as dialogue (default location is the downloads folder, default name is the title of the page)
  3. Save .webloc file to that location.

But it doesn't. No file is created with the code I created. The other codes I mentioned were just other less-ideal solutions (to saving a link as a file) to prove that I have been trying hard to find a way to do this myself! 

 

I tried the code you wrote (both with Safari and changing it to suit Google Chrome) in the Applescript Editor. When I run it, I'm asked where to save the file and what to name it (as expected). But when I click save, my input is disregarded and the .webloc file saves to the desktop with the name equal to the title of the web page. 

Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...