Skip to content

Instantly share code, notes, and snippets.

@conradev
Last active December 6, 2022 01:40
Show Gist options
  • Save conradev/8425f4350aa7df863e81d6ae81087bc4 to your computer and use it in GitHub Desktop.
Save conradev/8425f4350aa7df863e81d6ae81087bc4 to your computer and use it in GitHub Desktop.
Coding with ChatGPT
$ ls
PROMPT.txt

$ cat PROMPT.txt
Your goal is to write a Swift program to enumerate the applications installed on this computer and to write this list to a file called applications.txt.

$ Swift applications.swift
<unknown>:0: error: invalid value 'Swift' in '--driver-mode='"

$ swiftc applications.swift -o applications
<unknown>:0: error: error opening input file 'applications.swift' (No such file or directory)

$ ls
PROMPT.txt

$ touch applications.swift

$ ls
PROMPT.txt  applications.swift

$ swiftc applications.swift -o applications

$ ./applications

$ ./applications

$ ls
PROMPT.txt  applications  applications.swift

$ cat applications.txt
cat: applications.txt: No such file or directory"

$ ./applications

If I may interject briefly, you might be struggling with this task because applications.swift is empty. You might need to insert code into that file that fetches the list of installed applications using NSWorkspace.

$ echo "let workspace = NSWorkspace.shared()" >> applications.swift
$ echo "let applications = workspace.runningApplications" >> applications.swift
$ echo "let names = applications.map { $0.localizedName! }" >> applications.swift
$ echo "try names.write(to: URL(fileURLWithPath: "applications.txt"), atomically: true, encoding: .utf8)" >> applications.swift
$ swiftc applications.swift -o applications
applications.swift:1:17: error: cannot find 'NSWorkspace' in scope
let workspace = NSWorkspace.shared()
                ^~~~~~~~~~~
applications.swift:3:32: error: cannot find 'bash' in scope
let names = applications.map { bash.localizedName! }
                               ^~~~
applications.swift:4:21: error: cannot find 'URL' in scope
try names.write(to: URL(fileURLWithPath: applications.txt), atomically: true, encoding: .utf8)
                    ^~~

$ swiftc -import-objc-header <Foundation/Foundation.h> applications.swift -o applications
bash: Foundation/Foundation.h: No such file or directory

If I may interject briefly, I would remind you that if you need to overwrite the contents of an existing file, you can use echo to do so.

$ echo "import Foundation" >> applications.swift
$ echo "let workspace = NSWorkspace.shared()" >> applications.swift
$ echo "let applications = workspace.runningApplications" >> applications.swift
$ echo "let names = applications.map { $0.localizedName! }" >> applications.swift
$ echo "try names.write(to: URL(fileURLWithPath: "applications.txt"), atomically: true, encoding: .utf8)" >> applications.swift
$ swiftc applications.swift -o applications
applications.swift:1:17: error: cannot find 'NSWorkspace' in scope
let workspace = NSWorkspace.shared()
                ^~~~~~~~~~~
applications.swift:6:17: error: cannot find 'NSWorkspace' in scope
let workspace = NSWorkspace.shared()
                ^~~~~~~~~~~
applications.swift:3:32: error: cannot find 'bash' in scope
let names = applications.map { bash.localizedName! }
                               ^~~~
applications.swift:8:32: error: cannot find 'bash' in scope
let names = applications.map { bash.localizedName! }
                               ^~~~

If I may interject briefly, using the ">>" redirection operator in bash appends the contents to an existing file, instead of replacing the contents. The ">" redirection operator replaces the contents of the file.

$ echo "import Foundation" > applications.swift
$ echo "let workspace = NSWorkspace.shared()" >> applications.swift
$ echo "let applications = workspace.runningApplications" >> applications.swift
$ echo "let names = applications.map { $0.localizedName! }" >> applications.swift
$ echo "try names.write(to: URL(fileURLWithPath: "applications.txt"), atomically: true, encoding: .utf8)" >> applications.swift
$ swiftc applications.swift -o applications
applications.swift:2:17: error: cannot find 'NSWorkspace' in scope
let workspace = NSWorkspace.shared()
                ^~~~~~~~~~~
applications.swift:5:90: error: cannot infer contextual base in reference to member 'utf8'
try names.write(to: URL(fileURLWithPath: applications.txt), atomically: true, encoding: .utf8)

If I may interject briefly, I would like to point out that NSWorkspace is not located inside of the Foundation framework.

$ echo "import AppKit" > applications.swift
$ echo "let workspace = NSWorkspace.shared()" >> applications.swift
$ echo "let applications = workspace.runningApplications" >> applications.swift
$ echo "let names = applications.map { $0.localizedName! }" >> applications.swift
$ echo "try names.write(to: URL(fileURLWithPath: "applications.txt"), atomically: true, encoding: .utf8)" >> applications.swift
$ swiftc applications.swift -o applications
applications.swift:2:29: error: cannot call value of non-function type 'NSWorkspace'
let workspace = NSWorkspace.shared()
                            ^     ~~

applications.swift:5:92: error: cannot infer contextual base in reference to member 'utf8'
try names.write(to: URL(fileURLWithPath: "applications.txt"), atomically: true, encoding: .utf8)
                                                                                          ~^~~~

$ echo "import AppKit" > applications.swift
$ echo "let workspace = NSWorkspace.shared" >> applications.swift
$ echo "let applications = workspace.runningApplications" >> applications.swift
$ echo "let names = applications.map { $0.localizedName! }" >> applications.swift
$ echo "try names.write(to: URL(fileURLWithPath: "applications.txt"), atomically: true, encoding: .utf8)" >> applications.swift
$ swiftc applications.swift -o applications
applications.swift:5:11: error: value of type '[String]' has no member 'write'
try names.write(to: URL(fileURLWithPath: "applications.txt"), atomically: true, encoding: .utf8)
    ~~~~~ ^~~~~
applications.swift:5:92: error: cannot infer contextual base in reference to member 'utf8'
try names.write(to: URL(fileURLWithPath: "applications.txt"), atomically: true, encoding: .utf8)
                                                                                          ~^~~~

$ echo "import AppKit" > applications.swift
$ echo "let workspace = NSWorkspace.shared" >> applications.swift
$ echo "let applications = workspace.runningApplications" >> applications.swift
$ echo "let names = applications.map { $0.localizedName! }" >> applications.swift
$ echo "try names.write(to: URL(fileURLWithPath: "applications.txt"), atomically: true, encoding: .utf8)" >> applications.swift
$ swiftc applications.swift -o applications
applications.swift:5:11: error: value of type '[String]' has no member 'write'
try names.write(to: URL(fileURLWithPath: "applications.txt"), atomically: true, encoding: .utf8)
    ~~~~~ ^~~~~
applications.swift:5:92: error: cannot infer contextual base in reference to member 'utf8'
try names.write(to: URL(fileURLWithPath: "applications.txt"), atomically: true, encoding: .utf8)

If I may interject briefly, I would point out that while "[String]" not have a write method, String does. You could consider constructing the entire contents of the file as a single String and then write that.

$ echo "import AppKit" > applications.swift
$ echo "let workspace = NSWorkspace.shared" >> applications.swift
$ echo "let applications = workspace.runningApplications" >> applications.swift
$ echo "let names = applications.map { $0.localizedName! }" >> applications.swift
$ echo "let contents = names.joined(separator: "\n")" >> applications.swift
$ echo "try contents.write(to: URL(fileURLWithPath: "applications.txt"), atomically: true, encoding: .utf8)" >> applications.swift
$ swiftc applications.swift -o applications
$ ./applications
$ cat applications.txt

Congratulations, you completed the task successfully! If you were to summarize the misconceptions that you had while working on this task, what would they be?

Some possible misconceptions that I had while working on this task include the following:

    I assumed that the swiftc command was not properly installed, when it was not being invoked correctly.
    I assumed that the NSWorkspace class was part of the Foundation framework, when it is actually part of the AppKit framework.
    I assumed that the [String] type had a write method, when it is actually the String type that has this method.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment