XiaTools

Fetching Public IP Addresses in macOS Terminal via AppleScript

Updated 10 Oct 2026

Fetching your public IP address directly from the macOS Terminal using AppleScript is a powerful way to automate network diagnostics, log connection changes, or feed IP data into other local workflows. Instead of opening a web browser, you can query external lookup services and parse the result natively within your shell or automation scripts. This guide walks you through the exact syntax, commands, and script structures required to reliably retrieve your external IP address on macOS.

To quickly verify your current network visibility before writing scripts, you can use the What Is My IP tool to instantly inspect your public address, geolocation details, and connection headers.

Understanding Public IP Retrieval on macOS

Your local machine has a private IP address assigned by your local router (such as 192.168.1.50 or 10.0.0.15), but websites and remote servers see your public-facing IP address assigned by your Internet Service Provider (ISP). To fetch this public address from the command line, your Mac must send an HTTP request to an external server that echoes back the source IP it observes.

While Bash or Zsh handles this easily via curl, wrapping or executing these commands inside AppleScript allows you to integrate network checks into macOS automation tools like Script Editor, Keyboard Maestro, or native shell scripts.

Basic Shell Command for Public IP

Before combining AppleScript with terminal commands, let us look at the underlying shell command used to fetch a public IP address. Open your Terminal application and run the following command using curl:

curl -s https://api.ipify.org

Sample Output

203.0.113.45

The -s flag runs curl in silent mode, suppressing the progress meter and error messages so that only the raw IP address is returned in the output stream.

Writing the macOS Terminal AppleScript to Fetch Public IP

AppleScript can execute shell scripts using the do shell script command. This bridges the gap between macOS desktop automation and command-line networking utilities.

Step-by-Step Implementation

  1. Open the Script Editor application on your Mac (located in Applications > Utilities).
  2. Create a new document and select AppleScript as the language.
  3. Paste the following script into the editor:
set publicIP to do shell script "curl -s https://api.ipify.org"
display dialog "Your public IP address is: " & publicIP buttons {"Copy to Clipboard", "OK"} default button "OK"

if button returned of result is "Copy to Clipboard" then
    set the clipboard to publicIP
end if
  1. Click the Run button in the toolbar to execute the script.

When executed, the script makes an HTTPS call to the IP-retrieval service, captures the string, and presents it in a native macOS dialog box with an option to copy the result directly to your system clipboard.

Running AppleScript Directly from Terminal

If you prefer not to use the graphical Script Editor, you can execute AppleScript directly from the macOS Terminal using the osascript command interpreter. This method lets you test and deploy your automation routines entirely from the command line.

osascript -e 'do shell script "curl -s https://api.ipify.org"'

Sample Output

203.0.113.45

You can also assign this output to a shell variable for further scripting:

MY_IP=$(osascript -e 'do shell script "curl -s https://api.ipify.org"')
echo "The retrieved IP is: $MY_IP"

Alternative IP Lookup Endpoints

If your primary IP echo service is unreachable or blocked by a corporate firewall, you can swap the endpoint URL in your AppleScript. Here are reliable, plain-text IP echo services commonly used in scripts:

  • https://api.ipify.org (Returns IPv4 only)
  • https://api6.ipify.org (Returns IPv6 only)
  • https://ifconfig.me/ip (Returns IPv4 or IPv6 depending on network)
  • https://icanhazip.com (Returns clean text with trailing newline)

Updated AppleScript for IPv6 Support

If your network infrastructure utilizes IPv6 and you want to fetch your global IPv6 address, modify the curl endpoint in your AppleScript:

try
    set publicIPv6 to do shell script "curl -6 -s https://api6.ipify.org"
    display dialog "Your public IPv6 address is: " & publicIPv6
on error
    display dialog "Failed to retrieve IPv6 address. Check your connection."
end try

Comparison of IP Fetching Methods

Method Environment Pros Cons Best Use Case
Pure Zsh/Bash (curl) Terminal / Shell scripts Extremely fast, lightweight No native GUI dialogs Background cron jobs, CLI tools
AppleScript (osascript) Terminal / Automation Native macOS dialogs, clipboard integration Slightly heavier execution overhead User-facing scripts, desktop alerts
Automator / Shortcuts macOS GUI Visual workflow building Less flexible version control Quick desktop applets

Common Mistakes and How to Fix Them

When running network scripts in macOS, you may occasionally run into roadblocks. Here are frequent issues and their solutions:

  • SSL Certificate Errors: If your corporate network intercepts SSL traffic using deep packet inspection, curl may throw a certificate validation error. Fix this temporarily in testing by adding the -k flag (curl -ks ...), though fixing the local keychain trust store is recommended for production.
  • Timeout Issues: Slow DNS lookups or unresponsive endpoints can hang your script. Always wrap your network commands in try blocks in AppleScript, or add a timeout parameter to curl: curl -s --max-time 5 https://api.ipify.org.
  • JSON Parsing Overhead: Avoid using heavy endpoints that return JSON unless you need extra metadata (like ISP or country). Sticking to plain-text endpoints prevents the need to write complex JSON parsing logic inside AppleScript.

Quick Checklist for Implementation

  • Test the raw shell command (curl -s https://api.ipify.org) in Terminal to confirm internet access.
  • Wrap the command in do shell script inside AppleScript Editor.
  • Add error handling (try ... on error) to manage offline scenarios gracefully.
  • Use osascript if you need to trigger the script from a standard shell script or cron job.
  • Verify your public IP details against an independent checker if troubleshooting network anomalies.

Frequently asked questions

Can I run this AppleScript without opening the Terminal app?

Yes. You can save the AppleScript as an application or script bundle using the Script Editor. Once saved, you can double-click it to run it like any other Mac app, or assign it a keyboard shortcut using native macOS settings or third-party automation tools.

Does this script work on both IPv4 and IPv6 networks?

By default, standard endpoints like ipify.org return your public IPv4 address. If you want to fetch an IPv6 address, you must use an IPv6-specific endpoint URL or pass the `-6` flag to curl within your AppleScript command.

What happens if my Mac is offline when the script runs?

If your Mac has no internet connection or the lookup service is down, the curl command will fail and throw a shell error. To prevent your script from crashing, always wrap your execution block in an AppleScript `try` statement.

Is it safe to use third-party APIs like ipify in scripts?

Yes, public IP echo services are standard for command-line scripts and do not log sensitive personal data—they simply echo back the source IP address of your incoming HTTP request. However, avoid sending confidential payloads to these endpoints.

How can I log the fetched IP address to a local file automatically?

You can redirect the output of your curl command directly to a text file in your shell script or AppleScript. For example, use the shell command `curl -s https://api.ipify.org >> ~/Documents/ip_log.txt` inside your `do shell script` block.

Related articles

Free tools