0

I'm trying to make an applescript that will write itself into a new window of Applescript Editor. But I can't figure out how it will paste itself with quotations. The code I have right now is

tell application "System Events"
    keystroke "n" using command down
    keystroke "tell application (System Events) keystroke (n) using command down end tell"
end tell
  • I deleted my original answer earlier today but have now edited and undeleted it. Hope it helps. – user3439894 Sep 27 '16 at 22:57
  • You need to clearly explicitly and specifically what it is your trying to do here, because as you can see the two answers given so far are looking at your question from two entirely unrelated directions. – user3439894 Sep 28 '16 at 2:02
1

Quotes that are meant to be literal need to be escaped with a backslash and instead of using keystroke, use the following example to create a new AppleScript document containing the wanted code:

tell application "AppleScript Editor"
    set text of (make new document) to "tell application \"System Events\"\n\tkeystroke \"n\" using command down\nend tell"
end tell

Copy and paste the code example above into a new AppleScript document and press the Compile button. It will then look like:

tell application "AppleScript Editor"
    set text of (make new document) to "tell application \"System Events\"
    keystroke \"n\" using command down
end tell"
end tell

Then press the Run button and it will create a new AppleScript document containing the following:

tell application "System Events"
    keystroke "n" using command down
end tell

Which if you then press the Run button of this new document, it creates a new blank AppleScript document.

Note: Depending on the version of OS X/macOS the tell application "AppleScript Editor" line may be tell application "Script Editor". You can still paste the example code, as is, into Script Editor and is will automatically remove the Apple portion from AppleScript to show as tell application "Script Editor".

  • Though your answer may work, reading the question once more I got the impression the major goal of the OP is an AppleScript which replicates itself: a quine. I'm no mathematician nor a specialist for theoretical computer science but I assume there are programming languages which can accomplish this and others can't. AppleScript probably belongs to the latter. – klanomath Sep 28 '16 at 0:38
  • @klanomath, The OP stated two things, "I'm trying to make an applescript that will write itself into a new window of Applescript Editor." and "But I can't figure out how it will paste itself with quotations." neither of which mention self-replicating nor any other parameters of how/what is trying to be accomplished. So until the OP comments, it's anyones guess to what the real intension is. – user3439894 Sep 28 '16 at 1:17
0

You are probably looking for an AppleScript quine. I'm no programmer nor a computer scientist so I can't really explain how to write self-reproducing code. Additional I don't know if a (high-level) script language like AppleScript is powerful enough to produce arbitrary quines.

I have found only two examples of working AppleScript quines which may help you to create your own one - only the first one creates a new AppleScript:

set d to "on string_from_ASCII_numbers(x)
    set s to ASCII character of item 1 of x
    repeat with i from 2 to number of items in x
        set s to s & (ASCII character of item i of x)
    end repeat
end string_from_ASCII_numbers
set set_d_to to {115, 101, 116, 32, 100, 32, 116, 111, 32}
set scriptEditor to {83, 99, 114, 105, 112, 116, 32, 69, 100, 105, 116, 111, 114}
set quine to string_from_ASCII_numbers(set_d_to) & quote & d & quote & return & d
tell application string_from_ASCII_numbers(scriptEditor) to make new document with properties {contents:quine}"
on string_from_ASCII_numbers(x)
    set s to ASCII character of item 1 of x
    repeat with i from 2 to number of items in x
        set s to s & (ASCII character of item i of x)
    end repeat
end string_from_ASCII_numbers
set set_d_to to {115, 101, 116, 32, 100, 32, 116, 111, 32}
set scriptEditor to {83, 99, 114, 105, 112, 116, 32, 69, 100, 105, 116, 111, 114}
set quine to string_from_ASCII_numbers(set_d_to) & quote & d & quote & return & d
tell application string_from_ASCII_numbers(scriptEditor) to make new document with properties {contents:quine}

Download link: quine.applescript.zip

set s to "on run {s}
  set pre to «data utxt0073006500740020007300200074006f00200022» as text
  set post to «data utxt0022000a00720075006e0020007300630072006900700074002000730020007700690074006800200070006100720061006d006500740065007200730020007b0073007d»
  pre & s & post
end run"
run script s with parameters {s}

Download link: quine.scpt

  • The second one doesn't really do much of anything and the first one fails in AppleScript Editor as it's apparently coded for Script Editor. The OP does say "new window of Applescript Editor" not "new window of Script Editor" – user3439894 Sep 28 '16 at 1:34
  • To work with AppleScript Editor vs. Script Editor replace both set scriptEditor to {83, 99, 114, 105, 112, 116, 32, 69, 100, 105, 116, 111, 114} lines with set scriptEditor to {65, 112, 112, 108, 101, 83, 99, 114, 105, 112, 116, 32, 69, 100, 105, 116, 111, 114}. – user3439894 Sep 28 '16 at 1:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .