Monday, July 23, 2018

#1 2014-07-16 05:29:33 am

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

Html easy reading

Hi,
everything you need to know is written in the comments in the script below. Did a revision, now everything works fine.

Applescript:

#Html easy reading
#System Events
#0,85
#Developed by Joy, 13.07.14, ->14.07.14
#=====================================
#HELP:
#
#Ages passed, since i started the first time to save regularly html pages on my Mac, for a later reading.
#Struggling…all the time…with the clutter that html pages generated (document + resource folder), i used different web browsers and these all saved in the same manner. Except Safari, with its webarchive format.
#probably you noted that i write in past-time. In fact, now i've the solution at hand, to read effortless htm, html files without all the clutter that comes with it.
#
#HOW MY SCRIPT WORKS: (in SHORT)
#-it moves html documents and resource folders to an unique folder, stored inside: Home/Library/HTM_Lib/
#-it creates soft links of all the moved htm, html documents back to your original folder.
# You can move, duplicate your soft links as you wish, because original html files and resources are stored in a save place
#
#HOW MY SCRIPT WORKS: (in LONG)
#- open a folder, containing html files, without to select any file inside your container (icon view) or select a folder containing html files (column view, should work also in list/slide view)
#- activate my script
#- files and folders become filtered as htm, html documents and their respective ressources folders (with suffix '_files'), excluding alias files (soft Links)
#- now this filtered files and folders are been moved into a particular library: Home/Library/HTM_Lib/ and there, in a subfolder who inherits the name from the original folder from which your html files came, plus a date string to create a unique name
#- now the script will start to create alias links (soft links) from all the previous moved html, htm documents continuing to restore readable html files in your original folder.
#- finally, the script creates a hard link of the subfolder in Home/Library/HTM_Lib/ containing your originals in your original folder, just to have the originals by hand. Delete that folder if you like.
#- And…voila! Enjoy ! reading html was never easier.
#
#
#BONUS:
#A last thing to pay attention: if you select an alias file/s, you'll be prompted if you wish to copy the original files to a directory of your choice
#
#My script isnt that spectacular, but very effective, and developed for everyday use. Try it out to believe !
#
#=====================================
#This script is: -free Software- use at your own risk
#Original Code from joy - Visit me on: [url]http://gothicage.deviantart.com/[/url]
#=====================================

on run
   #get frontmost folder
   tell application "Finder"
       close every information window #interferes
       set proj_fl to target of window 1 as text
       
       #select alias files to save original files to another location
       set sel to the selection
       if class of item 1 of sel is alias file then
           my get_original(sel)
       end if
       
       #return proj_fl
       set nm_wind to name of alias proj_fl
   end tell
   
   #get and correct finds (no links, find only in current dir)
   set {html_d1, html_d2, html_fl} to my find_files(proj_fl)
   
   set htm_doc_cc to (number of items in html_d1) + (number of items in html_d2)
   set cc_fl to number of items in html_fl
   
   if cc_fl = htm_doc_cc then
       set trace_ky to false
       #transform found paths into HFS paths
       set ls1 to {}
       repeat with a in html_d1
           copy POSIX file a as alias to end of ls1
       end repeat
       set ls2 to {}
       repeat with a in html_d2
           copy POSIX file a as alias to end of ls2
       end repeat
       
   else if cc_fl < htm_doc_cc or cc_fl > htm_doc_cc then
       set trace_ky to true
       #not alias file
       #transform into HFS paths and strip away invalid files
       set ls1 to {}
       set trace_1 to {}
       if html_d1 is not {} then
           repeat with a in html_d1
               set hfs_f to POSIX file a as text
               tell application "Finder" to if class of alias hfs_f is document file then
                   copy name of alias hfs_f to end of trace_1
                   copy alias hfs_f to end of ls1
               end if
           end repeat
       end if
       set ls2 to {}
       set trace_2 to {}
       if html_d2 is not {} then
           repeat with a in html_d2
               set hfs_f to POSIX file a as text
               tell application "Finder" to if class of alias hfs_f is document file then
                   copy name of alias hfs_f to end of trace_2
                   copy alias hfs_f to end of ls2
               end if
           end repeat
       end if
   end if
   #sum lists
   set all_html to ls1 & ls2
   
   set ls3 to {}
   repeat with a in html_fl
       copy POSIX file a as alias to end of ls3
   end repeat
   
   #sort htm+ressources in date-folders year, month, day -better folder hierarchy
   set sh_dt to short date string of (current date)
   set {dd, mm, yy} to words of find_rep(sh_dt, ".", " ")
   set dt_fl to (yy & "-" & mm & "-" & dd as text)
   
   #prepare store folder
   set db_pt to ((path to library folder from user domain as text) & "HTM DB:" & dt_fl & " " & nm_wind & ":" as text)
   do shell script "mkdir -p '" & POSIX path of db_pt & "'"
   
   
   #move ressources-finder is comfortable, but a bit lame
   tell application "Finder"
       #move clutter into its db
       move items in ls3 to alias db_pt
       do shell script "sleep 0.5" # & cc
       move items in all_html to alias db_pt
       do shell script "sleep 0.5" # & cc
       
       #finally, create readable alias links
       set htm_files to document files of alias db_pt
       if trace_ky is false then
           repeat with orig_file in htm_files
               make new alias file of proj_fl to orig_file
           end repeat
       else
           repeat with orig_file in htm_files
               set nm to name of orig_file
               if nm is in trace_1 or nm is in trace_2 then
                   make new alias file of proj_fl to orig_file
               end if
           end repeat
       end if
   end tell
   
   #create a symbolic link to originals
   set cmd to "ln -s '" & POSIX path of db_pt & "' '" & POSIX path of (proj_fl & dt_fl & " " & nm_wind as text) & "'"
   #log cmd
   try
       do shell script cmd
   end try
   
end run

on get_original(s)
   tell application "Finder"
       #set s to the selection as list
       #if s is {} then return
       activate
       display dialog "Copy Originals from Selection ?" with icon 1 buttons { "Copy", "Cancel"} default button 1
       set the_b to the button returned of the result
       if the_b is "Copy" then
           #activate
           set targ_pt to choose folder default location alias (path to home folder as text) with prompt "Copy to…"
           #copies overwrite automatically alias files
           
           try
               set pkg_dir to (targ_pt & "HTM Export:" as text)
               #set pkg_dir to (targ_pt & short_nm as text)
               do shell script "mkdir -p '" & POSIX path of pkg_dir & "'"
           on error err
               display dialog err giving up after 5
               #exit repeat
           end try
           repeat with a in s
               if class of a is alias file then
                   set {orig_sx, orig_nm, get_prt} to {name extension, name, parent} of original item of a
                   set short_nm to text 1 thru -((length of orig_sx) + 2) of orig_nm
                   set orig_ress to ((get_prt as text) & short_nm & "_files" as text)
                   #try
                   duplicate alias (original item of a as text) to alias pkg_dir
                   duplicate alias orig_ress to alias pkg_dir
                   #end try
               end if
           end repeat
           
       else if the_b is "Cancel" then
           return
       end if
   end tell
   
end get_original


on find_files(proj_fl)
   #get and correct finds (no links, find only in current dir)
   set {fn, rep} to {"//", "/"}
   set html_d1 to (do shell script "find '" & POSIX path of proj_fl & "' -d 1 -name \"*.html\" ! -type l")
   set html_d1 to paragraphs of my find_rep(html_d1, fn, rep)
   
   set html_d2 to (do shell script "find '" & POSIX path of proj_fl & "' -d 1 -name \"*.htm\" ! -type l")
   set html_d2 to paragraphs of my find_rep(html_d2, fn, rep)
   
   set html_fl to (do shell script "find '" & POSIX path of proj_fl & "' -d 1 -name \"*_files\"")
   set html_fl to paragraphs of my find_rep(html_fl, fn, rep)
   return {html_d1, html_d2, html_fl}
end find_files

on find_rep(T, fn, rep)
   set tid to text item delimiters
   set text item delimiters to fn
   set T to text items of T
   set text item delimiters to rep
   set T to T as string
   set text item delimiters to tid
   return T
end find_rep

Last edited by Joy (2014-07-16 09:11:40 am)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)