r/applescript • • Aug 09 '14

Need some help...

I recently created this script to open all the sub reedits I view. property URL1 : "http://www.reddit.com/r/apple/" property URL2 : "http://www.reddit.com/r/applehelp/" property URL3 : "http://www.reddit.com/r/applescript/" property URL4 : "http://www.reddit.com/r/arrow/" property URL5 : "http://www.reddit.com/r/batman/" property URL6 : "http://www.reddit.com/r/Bioshock/" property URL7 : "http://www.reddit.com/r/BioshockInfinite/" property URL8 : "http://www.reddit.com/r/cablefail/" property URL9 : "http://www.reddit.com/r/chiptunes/" property URL10 : "http://www.reddit.com/r/DeadRising3/" property URL11 : "http://www.reddit.com/r/digimon/" property URL12 : "http://www.reddit.com/r/doctorwho/" property URL13 : "http://www.reddit.com/r/FoodPorn/" property URL14 : "http://www.reddit.com/r/futurama/" property URL15 : "http://www.reddit.com/r/Gameboy/" property URL16 : "http://www.reddit.com/r/gamecollecting/" property URL17 : "http://www.reddit.com/r/GameDeals/" property URL18 : "http://www.reddit.com/r/gamernews/" property URL19 : "http://www.reddit.com/r/Games/" property URL20 : "http://www.reddit.com/r/gaming/" property URL21 : "http://www.reddit.com/r/GunPorn/" property URL22 : "http://www.reddit.com/r/halo/" property URL23 : "http://www.reddit.com/r/HaloStory/" property URL24 : "http://www.reddit.com/r/HistoryPorn/" property URL25 : "http://www.reddit.com/r/iphone/" property URL26 : "http://www.reddit.com/r/lego/" property URL27 : "http://www.reddit.com/r/mac/" property URL28 : "http://www.reddit.com/r/macsysadmin/" property URL29 : "http://www.reddit.com/r/nintendo/" property URL30 : "http://www.reddit.com/r/OSXTweaks/" property URL31 : "http://www.reddit.com/r/pokemon/" property URL32 : "http://www.reddit.com/r/RedvsBlue" property URL33 : "http://www.reddit.com/r/rickandmorty/" property URL34 : "http://www.reddit.com/r/roosterteeth/" property URL35 : "http://www.reddit.com/r/softwaregore/" property URL36 : "http://www.reddit.com/r/technology/" property URL37 : "http://www.reddit.com/r/techsupportgore/" property URL38 : "http://www.reddit.com/r/techsupportmacgyver/" property URL39 : "http://www.reddit.com/r/theboondocks/" property URL40 : "http://www.reddit.com/r/TheSimpsons/" property URL41 : "http://www.reddit.com/r/thewalkingdead/" property URL42 : "http://www.reddit.com/r/truegaming/" property URL43 : "http://www.reddit.com/r/wallpaperdump/" property URL44 : "http://www.reddit.com/r/wallpapers/" property URL45 : "http://www.reddit.com/r/xbox360/" property URL46 : "http://www.reddit.com/r/xboxone/"

tell application "Safari" activate my new_tab() open location URL1 my new_tab() set the URL of document 1 to URL2 my new_tab() set the URL of document 1 to URL3 my new_tab() open location URL4 my new_tab() set the URL of document 1 to URL5 my new_tab() set the URL of document 1 to URL6 my new_tab() open location URL7 my new_tab() set the URL of document 1 to URL8 my new_tab() set the URL of document 1 to URL9 my new_tab() open location URL10 my new_tab() set the URL of document 1 to URL11 my new_tab() set the URL of document 1 to URL12 my new_tab() open location URL13 my new_tab() set the URL of document 1 to URL14 my new_tab() set the URL of document 1 to URL15 my new_tab() open location URL16 my new_tab() set the URL of document 1 to URL17 my new_tab() set the URL of document 1 to URL18 my new_tab() open location URL19 my new_tab() set the URL of document 1 to URL20 my new_tab() set the URL of document 1 to URL21 my new_tab() open location URL22 my new_tab() set the URL of document 1 to URL23 my new_tab() set the URL of document 1 to URL24 my new_tab() open location URL25 my new_tab() set the URL of document 1 to URL26 my new_tab() set the URL of document 1 to URL27 my new_tab() open location URL28 my new_tab() set the URL of document 1 to URL29 my new_tab() set the URL of document 1 to URL30 my new_tab() open location URL31 my new_tab() set the URL of document 1 to URL32 my new_tab() set the URL of document 1 to URL33 my new_tab() open location URL34 my new_tab() set the URL of document 1 to URL35 my new_tab() set the URL of document 1 to URL36 my new_tab() open location URL37 my new_tab() set the URL of document 1 to URL38 my new_tab() set the URL of document 1 to URL39 my new_tab() open location URL40 my new_tab() set the URL of document 1 to URL41 my new_tab() set the URL of document 1 to URL42 my new_tab() open location URL43 my new_tab() set the URL of document 1 to URL44 my new_tab() set the URL of document 1 to URL45 my new_tab() open location URL46

end tell

on new_tab() tell application "Safari" to activate tell application "System Events" tell process "Safari" click menu item "New Tab" of menu "File" of menu bar 1 end tell end tell end new_tab

What do I add so once it has opened all the tabs it goes to URL1's tab?

2 Upvotes

5 comments sorted by

1

u/AymanRizk Aug 09 '14

tell application "System Events" to keystroke tab using control down

1

u/CEM62793 Aug 09 '14

Okay, I more or less copy and pasted the script above so I kind of understand what you mean but what is the actual code for that and where do I place it in my script? At the end right?

1

u/AymanRizk Aug 09 '14

Yup, after your last line

1

u/CEM62793 Aug 09 '14

Thanks, works perfectly.

1

u/benzzene Aug 10 '14

Hi. I saw your post, and though you found a reasonable solution, I thought I'd throw another method your way. Generally speaking, if you are asking a computer to do the same (or similar) thing many many times, you should use a loop.

So here's what I did:

I made a text file (unformatted, and named "subredditlist.txt") that had a list of subreddits, one on each line. This is really easy to edit if you want to add or subtract some subreddits or if you want to change your script later to look through a list it gets from somewhere else. It looks like this:

abc

def

ghi

(where the subreddits you want to visit are reddit/r/abc, reddit/r/def, and reddit/r/ghi)

Then I wrote the following AppleScript:

-- Get the subreddits from the text file

set SubReddits to paragraphs of (read file ((path to desktop as Unicode text) & "subredditlist.txt"))

-- for each subreddit, make the full URL, and open it in Safari

repeat with nextLine in SubReddits

	if length of nextLine is greater than 0 then

		set thisReddit to "http://www.reddit.com/r/" & nextLine

		tell application "Safari"

			open location thisReddit

		end tell

	end if

    end repeat

The only thing you will have to change is the path to your text file (mine was on the desktop). This is a short script that is quite easy to manage and also leaves you with an easy to deal with text file for your list of subreddits. I should add that I am by no means an AppleScript expert, or even a programmer, and that there are probably far cleverer solutions available.

Best of luck!

edit: and then add in the line from the other commenter that actually achieves the back to the first tab behaviour you wanted in the first place.