OpenAI - ChatGPT Generates AppleScript

I came across this OpenAI ChatGPT interface for the Mac:

I couldn’t resist:

I think me and Shane are done now - you can all look after yourselves :slight_smile:

NOTE: you’ll need an OpenAI account and you should be aware of the OpenAI privacy statement.

5 Likes

For those that didn’t notice, the suggested code is incorrect and should be:

tell application "Finder"
	duplicate file "file.txt" to desktop
end tell
1 Like

This is better (after indicating the first response was wrong):

3 Likes

Hi all,

Just signed up to the forum.

I tried the chatGPT myself and some of subpages where you can instruct the OpenAI to follow the instructions and create AppleScripts even including ASobjC which I only know superficial, but I think that OpenAI appears to have a least some idea of how to write the scripts even if I double that it can ever run without a proper overhal by some of you guys here in this forum

Excuses for the poor formating of the script, I will try to improve on this going forward

Ole

—— text for the OpenAI begins here —-

Convert this text to AppleScript including use of ASobjC

Example: create an AppleScript including ASobjC with “use framework” commands
Output: a complete script that follows the below instruction

instruction:
Make an Applescript that includes ASobjC
The script shall prompt the user for an unlocked pdf file
The chosen pdf file shall be encrypted with a blank owner password and user password “test”
With permissions to disallow changes, allow printing, allow annotations

use framework "Foundation"
use scripting additions

set theFile to choose file with prompt "Choose an unlocked PDF file:"

set theURL to current application's NSURL's fileURLWithPath:(POSIX path of theFile)

set thePDFDoc to current application's PDFDocument's alloc()'s initWithURL:theURL

tell thePDFDoc
	set documentPermissions to current application's PDFPermission's alloc()'s initWithOwnerPassword:"" userPassword:"test"
	set allowsChanges to false
	set allowsPrinting to true
	set allowsAnnotations to true
end tell

thePDFDoc's writeToURL:theURL withOptions:(current application's NSDictionary's dictionaryWithObject:documentPermissions forKey:"Permissions")

display dialog "The PDF file has been encrypted successfully!" buttons {"OK"} default button 1
2 Likes

I find it fascinating how close the AI gets to creating useful code. There are other coding focused AIs, such as GitHub’s Copilot, which may be useful too.

I’ve been thinking a lot about ways to mine MacScripter.net and this forum to offer suggested solutions for common problems.

AppleScript seems to me to be a particularly good place to apply these tools as a shortcut to finding magic incantation to accomplish a particular outcome.

As these tools mature there may be opportunities for integration into SD, though that seems some distance off.

2 Likes

Here’s my very first attempt:

Not bad at all!

1 Like

I guess that in time we will come to take this stuff for granted just as we do with every other technology but it really is quite incredible.

It says it can’t search the Internet, so forum posts must be part of the curated data it’s trained on.

1 Like

Agreed.

This Hacker News thread is lengthy but there are some fascinating things buried in there:

The end game for this would be the replacement of AppleScript as the user interface for automation with this kind of narrative interface where you describe what needs to happen and the charBot directs your Mac to accomplish the task using a range of automation tools (AppleScript, bash, Automator, etc.). I.E. a SIRI that actually works.

I was awkwardly trying to get at this in this post:

4 Likes

AppleScript is well compatible with natural language. So, I wrote Siri-like natural language interface “Newt Off” in 2002.

General speaking, natural language interface depends on how user is intelligent.
Poor words brings natural language interface more poor function.

3 Likes

The below link may be of interest for those of you following this thread

1 Like

Here’s an interesting article on the possibilities of systems link ChatCPT in general:

I could see how this would be useful…

show a regular expression to select every paragraph with a number in it

Here is a regular expression that will match any paragraph that contains at least one digit:

```(?<=<p>)[^<]*\d[^<]*(?=</p>)```
This regular expression uses positive lookbehind and positive lookahead to ensure that the matched text is contained within <p> tags. The \d character class matches any digit, and the [^<]* pattern matches any number of characters that are not <.

Here is an example of how you could use this regular expression in Python:

html = '''
<html>
  <body>
    <p>This is a paragraph without a number.</p>
    <p>This is a paragraph with a number: 42.</p>
    <p>This is another paragraph with a number: 123.</p>
  </body>
</html>
'''

regex = r'(?<=<p>)[^<]*\d[^<]*(?=</p>)'

for match in re.finditer(regex, html):
  print(match.group())

and in AppleScript…

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use script "RegexAndStuffLib" version "1.0.7"
set myText to "
<html>
  <body>
    <p>This is a paragraph without a number.</p>
    <p>This is a paragraph with a number: 42.</p>
    <p>This is another paragraph with a number: 123.</p>
  </body>
</html>
"
set theRegex to "(?<=<p>)[^<]*\\d[^<]*(?=</p>)"

set regExResult to regex search myText ¬
   search pattern theRegex ¬
   

(Clarification, the appleScript version was me rewriting the ChatGPT Python sample)

So ChatGPT generated this snippet in Objective-C

It’s intended to toggle display mirroring.

#import <IOKit/graphics/IOGraphicsLib.h>

io_service_t display = CGDisplayIOServicePort(kCGDirectMainDisplay);
int mirror = 1; // 1 to enable display mirroring, 0 to disable
IODisplaySetIntegerParameter(display, kNilOptions, CFSTR(kIODisplayMMirrorKey), mirror);

How would I invoke this command via ASobjC? (or any other method?)

I tested: show me AppleScript to count mail

tell application “Mail”
set messageCount to count of messages in inbox
return messageCount
end tell

Next: show me how to use PyObjC

from Foundation import *
from ScriptingBridge import *

mail = SBApplication.applicationWithBundleIdentifier_(“com.apple.Mail”)
inbox = mail.inboxes()[0]
message_count = inbox.messages().count()
print(message_count)

Next: show me how to use ASObjC

import ASObjC

let mail = SBApplication(bundleIdentifier: “com.apple.Mail”)!
let inbox = mail.inboxes().firstObject as! MailInbox
let messageCount = inbox.messages().count
print(messageCount)

The last one looks like Swift :slight_smile:

Next: show me how to use AppleScriptObjC

use framework “Foundation”
use scripting additions

set mail to current application’s SBApplication’s applicationWithBundleIdentifier:“com.apple.Mail”
set inbox to mail’s inboxes()'s firstObject()
set messageCount to inbox’s messages()'s count()
return messageCount

If I use AppleScriptObjC it works but not ASObjC :slight_smile:

ChatGPT sent me down the garden path. TL;DR it got the answer wrong and told me that it’s possible to send a handler as a parameter to another handler (with example code!).

I tested Google’s new Gemini AI app–which is thought to be a competitor to ChatGPT–to see how it would do writing simple (and not so simple) AppleScripts.

The first script request was to merge PDF files. The Gemini solution was to open the source PDFs in the Preview app and to merge them in a new document, but the script did not work. I don’t think the script could be edited to work, but I didn’t spend too much time on it.

The second script request was to tile windows. It didn’t work but could be made to work, although generally speaking the script was a bit of a mess:

tell application "System Events"
  set screenWidth to size of screen 1
  set screenHeight to size of screen 1
  tell application processes
    repeat with thisProcess in every process
      set windows to every window of thisProcess
      repeat with thisWindow in windows
        set windowWidth to (screenWidth / (count of windows)) - 10
        set windowHeight to screenHeight
        set position of thisWindow to {(windowWidth * (index of thisWindow)) + 10, 0}
        set size of thisWindow to {windowWidth, windowHeight}
      end repeat
    end repeat
  end tell
end tell

The final script request was to bookmark a folder in a Finder window. Gemini’s solution was to add the selected folder to the Finder sidebar, but the script did not work and could not be edited to work.

Gemini seems to give better advice with the Shortcuts app, although this might be a result of the simple question posted:

While the Shortcuts app doesn’t have a dedicated “copy” function, you can achieve the same result using the “Save File” action. Here’s a quick guide:

  • Open the Shortcuts app and create a new shortcut.
  • Search for “Save File” and add it to your shortcut.
  • In the “Save File” action, tap on “File” and select the file you want to copy.
  • Choose where you want to save the copied file. You can select a specific folder or allow the user to choose during execution.
  • Optionally, rename the copied file using the “Text” action before saving.
    That’s it! Run the shortcut to copy the selected file to the chosen location.