Apple Script - Take User Input and Open URLs (replacing part of context URL with user input string) in Safari

Joined
Nov 16, 2020
Messages
3
Reaction score
0
Hi All -

I am new user of MacBook and new to Apple Scripting.

I am looking for help on Apple script which takes user input and open the URLs (by replacing string in URL with user input) in Safari tabs.

Let me explain what I am looking for. I have two URLs as follows:

https://www.mycompticketss.com/tickets/ticket-number
https://www.mycomptikcetss.com/tickets/ticket-number/history

In above URLs you will find ticket-number as an example, this ticket number should be replaced with what ever the user input number which is provided when running apple script. for e.g. if we run apple script and provide user input as 1234, apple script should open below URLs in safari tabs.


https://www.mycompticketss.com/tickets/1234
https://www.mycomptikcetss.com/tickets/1234/history

I have multiple URLs as above, I just listed two as an example.

Can someone let me know the apple script which can implement above for me?

thanks!
 
Joined
Jan 25, 2017
Messages
1,266
Reaction score
100
Something like this should do the job.
Code:
set ticketNumber to display dialog "Which ticket number?" default answer "" with icon note buttons {"Cancel", "Continue"} default button "Continue"

tell application "Safari" to activate

delay 0.5 -- Delay to allow time for Safari to open

tell application "Safari"

    make new document with properties {URL:"https://www.mycompticketss.com/" & (text returned of ticketNumber) & ".html"}

    tell window 1


        set current tab to (make new tab with properties {URL:"https://www.mycompticketss.com/" & (text returned of ticketNumber) & "/history.html"})

    end tell

end tell
 
Last edited:
Joined
Nov 16, 2020
Messages
3
Reaction score
0
Hi -

Thanks a lot for the details. The script you provided does not have any parsing errors. But when I try to make modifications with the ticketing URLs I have, I am getting syntax errors.

Let me provide more details. Below are some of my company ticketing URLs used for looking up tickets. 9999 is the ticket number as an example. As you can see the ticket number is at different position in context URL for different links. I have several other ticketing links, but all of the other links structure falls under one of these 4 links. So if I can make the script to work with below 4 structure URLs, I think I should be good, I think for each additional links I can just tell window/end tell loops in the script.

Can you please let me know how I can modify the script you provided (which prompts for ticket number) with below URLs?





thanks!
 
Joined
Jan 25, 2017
Messages
1,266
Reaction score
100
make new document with properties {URL:"https://www.mycompticketss.com/tickets" & (text returned of ticketNumber)


make new document with properties {URL:"https://www.mycompticketss.com/tickets-history/" & (text returned of ticketNumber) & "-ticket-all/"}

make new document with properties {URL:"https://www.mycompticketss.com/tickets-details/report/" & (text returned of ticketNumber) & ""survey-rating"}

make new document with properties {URL:"https://www.mycompticketss.com/tags/?ticket=" & (text returned of ticketNumber) }
 
Joined
Nov 16, 2020
Messages
3
Reaction score
0
Hi -

Thanks for the response.

9999 is a target file and not a directory.

Can you please post the complete snippet of apple script code so that I can test it?

Thanks!
 
Joined
Jan 25, 2017
Messages
1,266
Reaction score
100
Method 1 will open safari and show the urls in 4 separate tabs.
Method 2 will open safari and show the urls in 4 separate browser windows.
Method 1.
Code:
set ticketNumber to display dialog "Which ticket number?" default answer "" with icon note buttons {"Cancel", "Continue"} default button "Continue"

tell application "Safari" to activate

delay 0.5

tell application "Safari"

    make new document with properties {URL:"https://www.mycompticketss.com/" & (text returned of ticketNumber) & ".html"}

    tell window 1

        set current tab to (make new tab with properties {URL:"https://www.mycompticketss.com/tickets-history/" & (text returned of ticketNumber) & "-ticket-all/"})

    end tell

    tell window 1

        set current tab to (make new tab with properties {URL:"https://www.mycompticketss.com/ticket-details/report/" & (text returned of ticketNumber) & "/survey-rating"})

    end tell

    tell window 1

        set current tab to (make new tab with properties {URL:"https://www.mycompticketss.com/tags/?ticket=" & (text returned of ticketNumber)})

    end tell

end tell

Method 2.


Code:
set ticketNumber to display dialog "Which ticket number?" default answer "" with icon note buttons {"Cancel", "Continue"} default button "Continue"

tell application "Safari" to activate

delay 0.5

tell application "Safari"

    make new document with properties {URL:"https://www.mycompticketss.com/" & (text returned of ticketNumber) & ".html"}

    tell application "Safari"

        make new document with properties {URL:"https://www.mycompticketss.com/tickets-history/" & (text returned of ticketNumber) & "-ticket-all/"}

    end tell

    tell application "Safari"

        make new document with properties {URL:"https://www.mycompticketss.com/ticket-details/report/" & (text returned of ticketNumber) & "/survey-rating"}

    end tell

    tell application "Safari"

        make new document with properties {URL:"https://www.mycompticketss.com/tags/?ticket=" & (text returned of ticketNumber)}

    end tell

end tell
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top