Applescritp noobie..

Discussion in 'Mac Programming' started by jimmy43, Aug 21, 2009.

  1. jimmy43 macrumors member

    Joined:
    Apr 9, 2008
    #1
    Hey everyone, apparently it's easy to do this but I have no idea what i'm doing.

    I want to take snapshots of websites (they are only on my local machine) and make little images of them. There is a long list of them so I'd like to automate this.

    Some help?:)
     
  2. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #2
    This should work:

    Code:
    property N : 0
    
    on justDomain(URLvar)
    	
    	set TID to AppleScript's text item delimiters
    	set AppleScript's text item delimiters to "."
    	set dumVar to second text item of URLvar
    	set AppleScript's text item delimiters to TID
    	return dumVar
    	
    end justDomain
    
    
    tell application "Safari"
    	activate
    	
    	set URLList to {"http://www.yahoo.com/", "http://www.google.com/", "http://www.bing.com/"}
    	
    	repeat with u in URLList
    		set the URL of document 1 to u
    		
    		set N to N + 1
    		delay 5
    		
    		set picPath to ((POSIX path of (path to desktop)) & my justDomain(u) & " " & N & ".png") as string
    		do shell script "screencapture -tjpg " & quoted form of picPath
    			
    	end repeat
    		
    end tell
    Credit where credit's due department: You can thank TUAW for the bulk of the screen capture code.

    You need to replace my URLList with the sites you want. Put the URLs in quotes, separated by commas and surrounded by curly brackets. This script will break otherwise.

    There's one issue ... and that's the "delay 5." If there isn't some delay, the screencapture could grab the wrong web site. Ideally, there should be some tight repeat loop that compares the URL of the current window to the URL you want.

    Delay 5 works on my machine. ymmv.

    mt
     
  3. jimmy43 thread starter macrumors member

    Joined:
    Apr 9, 2008
    #3
    You are my hero! it works great. I now need to figure out how to call this from a php script and pass it that list of URLs.. but this is already nice.

    Thank you :D:D:D
     
  4. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #4
    Does it have to be PHP? In what form is your list of web sites? Chances are it can be done all in AppleScript ... which might save you some maintenance in the long run.

    mt
     
  5. jimmy43 thread starter macrumors member

    Joined:
    Apr 9, 2008
    #5
    Yes, so the php part dynamically scans the folder tree and creates links to these local websites. I wanted to have screenshots for these though so it's easier for people to preview the site. It's like a mini web-portal for people in my office.

    Now I'm trying to see if I can get applescript to do the same thing, i.e. scan the folder tree and generate previews for those sites... Any idea how i can get the set URLList to {"... part to just be somethign like
    set URLList to {"mylocalsite.1", "mylocalsite.2"} ..etc?:rolleyes:
     
  6. jimmy43 thread starter macrumors member

    Joined:
    Apr 9, 2008
    #6
    Actually I got it all working! Thanks for your help again. Here is my script:


    tell application "Safari"
    activate

    tell application "Finder"
    set URLList to get name of folders of folder ("/Volumes/.../" as POSIX file)
    end tell


    repeat with u in URLList
    set the URL of document 1 to "http://" & u

    delay 5

    set picPath to ("/Volumes/.../screenshots/" & u & ".jpg") as string
    do shell script "screencapture -tjpg " & quoted form of picPath

    end repeat

    end tell

    and run like so:

    sudo osascript screencapturerer.scpt

    Thanks again for all your help!!
     

Share This Page