Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pipwerks/9443477 to your computer and use it in GitHub Desktop.
Save pipwerks/9443477 to your computer and use it in GitHub Desktop.
AppleScript for converting "localhost" address to your Mac's current IP address. This enables testing in VMs (they can't resolve localhost but *can* resolve the local IP). This also enables your coworkers to view pages being served from your Mac. Read more at http://pipwerks.com/2014/03/08/convert-localhost-to-your-macs-current-ip-address/
(* functions *)
-- string replace function from http://www.macosxautomation.com/applescript/sbrt/sbrt-06.html
on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars
-- replaces specified domain with IP address in specified URLs
on replace_localhost(theDomain, theURL, theIP)
set newURL to ""
if theDomain is in theURL then
set newURL to replace_chars(theURL, theDomain, theIP)
else
set newURL to "http://" & theIP & "/"
end if
return newURL
end replace_localhost
-- copies URL to clipboard and ensures original app remains active (frontmost)
on copy_to_clipboard(theText, currentApp)
try
tell application "Finder"
set the clipboard to theText as text
-- display alert "\"" & (the clipboard) & "\" was copied to the clipboard"
end tell
tell application "System Events" to set frontmost of process currentApp to true
end try
end copy_to_clipboard
on run
-- If user has custom hostname, let's get it
display dialog "If you are using a custom localhost domain (via VirtualHostX or similar), please enter the domain now:" default answer "localhost"
set domain to text returned of result
-- empty list for placing localhost URLs
set localhostURLs to {}
-- placeholder for target URL
set selectedURL to ""
-- get name of current active app so we can switch back after getting IP address
tell application "System Events"
set currentApp to item 1 of (get name of processes whose frontmost is true)
end tell
(*
get the IP of localhost. try en0 first, then en1 if no result found
originally used ifconfig with grep, like so:
do shell script "ifconfig en0|grep \"inet \"|cut -d ' ' -f 2"
A shorter, more concise method was described in The Macintosh Terminal Pocket Guide by Daniel J. Barrett
do shell script "ipconfig getifaddr en0"
This method sometimes produces an error in AppleScript. To avoid the error, add ";exit 0"
"ipconfig getifaddr en0; exit 0"
See this URL for an explanation
http://lists.apple.com/archives/applescript-studio/2006/Jul/msg00141.html
*)
-- try en0 (if both wired and wireless are available, en0 is usually the wired connection)
set theIP to do shell script "ipconfig getifaddr en0; exit 0"
-- if en0 didn't work, try en1
if theIP is equal to "" then
set theIP to do shell script "ipconfig getifaddr en1; exit 0"
end if
-- if both en0 and en1 don't work, you're out of luck
if theIP is equal to "" then
display dialog "Sorry, your IP address couldn't be determined."
return
end if
(*
Check Google Chrome
Loop to find localhost (or custom domain)
Code adapted from http://superuser.com/questions/263198/switch-between-google-chrome-tabs-using-applescript
*)
if application "Google Chrome" is running then
tell application "Google Chrome"
set i to 0
try
repeat with t in (tabs of (first window))
set i to i + 1
if domain is in URL of t then
set localhostURLs to localhostURLs & URL of t
end if
end repeat
end try
end tell
end if
(*
Check Apple Safari
Loop to find localhost (or custom domain)
*)
if application "Safari" is running then
tell application "Safari"
set i to 0
try
repeat with t in (tabs of (first window))
set i to i + 1
if domain is in URL of t then
set localhostURLs to localhostURLs & URL of t
end if
end repeat
end try
end tell
end if
-- Clean up the URLs: replace all instances of "localhost" with IP
set i to 1
repeat with theURL in localhostURLs
set item i of localhostURLs to replace_localhost(domain, theURL, theIP)
set i to i + 1
end repeat
if (count of localhostURLs) is greater than 1 then
choose from list localhostURLs with prompt "Which URL would you like to copy?" OK button name "Copy" default items item 1 of localhostURLs
-- "cancel" button will return a value of false
if the result is not false then
set selectedURL to item 1 of the result as text
end if
if selectedURL is not "" then copy_to_clipboard(selectedURL, currentApp)
else if (count of localhostURLs) is equal to 1 then
set selectedURL to item 1 of localhostURLs as text
copy_to_clipboard(selectedURL, currentApp)
display dialog selectedURL & " has been copied to your clipboard"
end if
end run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment