Friday, July 20, 2018

#1 2014-07-30 01:47:33 pm

Joy
Member
From:: South Tirol
Registered: 2008-07-04
Posts: 557
Website

URLs to Finder comment

Hi,
it annoyed me very long time, to have no source url info attached to my offline readings, like htm, html documents.
After some discussion on the main forum, i decided to solve the problem with another approach.
The code below was written for my preferred browser, Opera. If some people want versions for Chrome, Safari, Firefox, then ask me here.But before you ask, try to make your hands dirty yourself, because the main code here works well. You really have to do some tweaks only, or take out the portions you need to build your own script. Enjoy!

Applescript:

#Source Url > Comment
#Opera
#0,4
#Developed by Joy, 30.07.14
#=====================================
#HELP:
#add source urls to downloaded files. Create an stay-open app or assign a keyboard shortcut to an Service, or other.
#
#RULES:
#a) set your browser under: property my_browser (i tested NOT with other browsers EXCEPT Opera, with absolutely no applescript support)
#b) choose short/long urls under: property: short_urls (true / false)
#c) set the file extension of the files you download. By default, htm is set, which means for spotlight: search for htm or html
#d) choose your download folder. By default, i instructed spotlight to look for downloaded htm, html documents in: Home/downloads/
# set your file path in paragraph 2 of the "on save" handler, if you like.
#=====================================

property my_browser : "Opera"
property short_urls : true
property sufx : ".htm" #dont forget the point!

on reopen
   my save_it()
end reopen

on run
   my save_it()
end run

on save_it()
   #activate your browser
   activate application my_browser
   
   #filepath
   set trg to (path to downloads folder as text)
   
   #fetch url and title
   set {ttitle, get_url} to my fetch_url(my_browser)
   if short_urls is true then
       set get_url to text 1 thru ((offset of "/" in get_url) - 1) of get_url
   end if
   #return {ttitle, get_url}
   
   #save process
   #repeat 2 times
   tell application my_browser to activate
   #end repeat
   #return
   my wait_r1(my_browser)
   set {bad_file, mdf} to my wait_r2(trg, ttitle)
   if bad_file is true then
       beep
       return
   end if
   
   set hfs_file to POSIX file (mdf as text)
   tell application "Finder" to set comment of (hfs_file as alias) to get_url
   
end save_it

on fetch_url(my_browser)
   tell application "System Events" to tell process my_browser to tell window 1
       tell text field 1 to set get_url to item 10 of (properties as list)
       set ttitle to title
   end tell
   return {ttitle, get_url}
end fetch_url

on wait_r1(my_browser)
   tell application "System Events"
       tell process my_browser to tell window 1
           keystroke "s" using command down
           do shell script "sleep 0.5"
           repeat
               if every sheet is {} then
                   exit repeat
               else
                   do shell script "sleep 2"
               end if
           end repeat
       end tell
   end tell
end wait_r1
on wait_r2(trg, ttitle)
   #routine for bigger files - wait till the file is written
   set dd to 0
   set bad_file to false
   set mdf to ""
   repeat
       set mdf to do shell script "mdfind -onlyin '" & POSIX path of trg & "' '" & ttitle & sufx & "'"
       if mdf is "" then
           do shell script "sleep 2"
           set dd to dd + 2
           if dd = 20 and mdf is "" then
               set bad_file to true
               exit repeat
           end if
       else
           exit repeat
       end if
   end repeat
   return {bad_file, mdf}
end wait_r2

Offline

 

#2 2014-09-19 05:19:08 am

Joy
Member
From:: South Tirol
Registered: 2008-07-04
Posts: 557
Website

Re: URLs to Finder comment

Sometimes i hate when apps upgrade.
The latest update of Opera broke my script. I tried with a valid workaround but it seems not to work regularly. So, my apologies, dont use this script anymore.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)