chatGPT AppleScript doesn't work

Hi guys,

im a noob when it comes to programming. I asked Chatgpt to write me an AppleScript where id like to push a certain button at an opened Page on Chrome.

The Code looks like this:

-- Ersetze hier den XPath des Buttons oder der URL, die du anklicken möchtest
set targetXPath to "//input[@value="Google Suche"]"

-- Schritt 1: Aktives Chrome-Fenster ermitteln
tell application "Google Chrome"
set targetWindow to front window
set targetTab to active tab of targetWindow
set targetTabURL to URL of targetTab
end tell

-- Schritt 2: Aktuelle Seite im Chrome-Fenster laden
tell application "Google Chrome"
tell targetTab
-- Hier kannst du optionale Aktionen ausführen, wie das Aktualisieren der Seite
-- zum Beispiel: reload
-- oder das Ausführen von JavaScript-Code
-- zum Beispiel: execute javascript "alert('Hello World!');"
end tell
end tell

-- Schritt 3: Warten, bis die Seite vollständig geladen ist
tell application "System Events"
tell process "Google Chrome"
-- Warte, bis das Fenster aktiv und geladen ist
repeat while (URL of active tab of front window is not equal to targetTabURL) or (loading of active tab of front window is true)
delay 0.5
end repeat
end tell
end tell

-- Schritt 4: Button oder URL mithilfe von XPath anklicken
tell application "System Events"
tell process "Google Chrome"
set targetElement to first UI element of front tab of front window whose value of attribute "AXXPath" is equal to targetXPath
perform action "AXPress" of targetElement
end tell
end tell

I Copy and pasted it as you can see in the Macro
test .kmmacros (3.8 KB)

Unfortunatly it doesnt work and i dont know why.. Can anyone help?

ChatGPT is not a good route to drafting AppleScript.

If you do want others to look at what you have so far, you will need to reduce the effort you impose on potential helpers by taking the trouble to enclose draft code in Code Fencing,

i.e. with preceding and following lines of three backticks each:

```

tell application "Google Chrome"
-- 等等
end tell
```

to make the code formatted and legible.


but you would make faster progress by skipping the ChatGPT stage entirely, and simply explaining what you are trying to get, from what inputs.

I want to learn how to push certain Buttons on the Web.

I need a Script, where i just need to Insert the Xpath of a Button of the Front Browser.

You don't need AppleScript for that.

This clicks the Keyboard Maestro icon on the top-left of this page, via its relative XPath:

Click Keyboard Maestro Icon.kmmacros (46 KB)

Macro screenshot

2 Likes

Im talking about Buttons that dont have an URL, like a connection Button in Linkedin for example

In Addition im using Chronium, so this Macro wont work Unfortunatly

Have you tried it? It should work with Chromium-based browsers (I use Brave). I don't know what Chronium is but it sounds like it might be...?

Are you able to get the relative XPath of the button? That's what this uses (not a URL).