Skip to content

Instantly share code, notes, and snippets.

@borkweb
Last active January 1, 2016 06:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save borkweb/8106232 to your computer and use it in GitHub Desktop.
Save borkweb/8106232 to your computer and use it in GitHub Desktop.
Automate the uploading of sabers to the Dark Jedi Brotherhood site so I don't have to do each one manually.
set saber to "Club"
set sabertrims to {"Plain", "Consular", "Guardian", "Krath", "Obelisk", "Sentinel", "Sith"}
set sabercolors to {"Blue", "Green", "Light Blue", "Purple", "Red", "Yellow"}
set sabertrims to {"Plain"}
set sabercolors to {"Pink", "Puce"}
set saberrequirements to {{"Rank", "13"}}
set theUrl to "https://www.darkjedibrotherhood.com/admin/sabers/new"
tell application "Google Chrome"
repeat with sabertrim in sabertrims
repeat with sabercolor in sabercolors
if (count every window) = 0 then
make new window
end if
set found to false
set theTabIndex to -1
repeat with theWindow in every window
set theTabIndex to 0
repeat with theTab in every tab of theWindow
set theTabIndex to theTabIndex + 1
if theTab's URL = theUrl then
set found to true
exit repeat
end if
end repeat
end repeat
if found then
set theWindow's active tab index to theTabIndex
set index of theWindow to 1
else
tell window 1
set newTab to make new tab with properties {URL:theUrl}
tell active tab
repeat -- wait until loading is complete
set curStat to loading
if curStat = false then exit repeat
delay 0.1
end repeat
end tell
end tell
end if
execute front window's active tab javascript "
var saber = '" & saber & "';
var trim = '" & sabertrim & "';
var color = '" & sabercolor & "';
$('#saber_hilt').val( saber );
$('#saber_trim').val( trim );
$('#saber_blade').val( color );
$('#saber_image').click();
"
set the lowersaber to my change_case(saber, "lower")
set the lowersabercolor to my change_case(sabercolor, "lower")
set the lowersabercolor to my trim(lowersabercolor)
set the lowersabertrim to my change_case(sabertrim, "lower")
set fileexists to "no"
set posixpath to "/Users/matt/Dropbox/djb/herald/sabers/" & lowersaber & "/" & lowersaber & "-" & lowersabertrim & "-" & lowersabercolor & ".png"
tell application "Finder" to if exists posixpath as POSIX file then set fileexists to "yes"
if fileexists is "no" then
display dialog "Could not find " & posixpath
return
end if
activate application "Google Chrome"
tell application "System Events" to tell process "Google Chrome"
keystroke "g" using {shift down, command down}
keystroke posixpath
delay 1
keystroke return
delay 1
keystroke return
end tell
set requirementcounter to 0
repeat with requirement in saberrequirements
set requirementtype to item 1 of requirement
set requirementvalue to item 2 of requirement
execute front window's active tab javascript "
var requirement = '" & requirementtype & "';
var value = '" & requirementvalue & "';
$('#saber_requirements_attributes_" & requirementcounter & "_requirement_type').val( requirement );
$('#saber_requirements_attributes_" & requirementcounter & "_requirement_id').val( value );
$('.add_fields').click();
"
set requirementcounter to requirementcounter + 1
end repeat
(*
execute front window's active tab javascript "
//$('#new_saber').submit();
"
*)
tell theWindow
tell theTab
repeat -- wait until loading is complete
set curStat to loading
if curStat = false then exit repeat
delay 0.1
end repeat
end tell
end tell
close theTab
end repeat
end repeat
end tell
on change_case(this_text, this_case)
if this_case is "lower" then
set the comparison_string to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set the source_string to "abcdefghijklmnopqrstuvwxyz"
else
set the comparison_string to "abcdefghijklmnopqrstuvwxyz"
set the source_string to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
end if
set the new_text to ""
repeat with thisChar in this_text
set x to the offset of thisChar in the comparison_string
if x is not 0 then
set the new_text to (the new_text & character x of the source_string) as string
else
set the new_text to (the new_text & thisChar) as string
end if
end repeat
return the new_text
end change_case
on trim(someText)
repeat until someText does not start with " "
set someText to text 2 thru -1 of someText
end repeat
repeat until someText does not end with " "
set someText to text 1 thru -2 of someText
end repeat
return someText
end trim
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment