(Solved) Copy string within Aseprite extension

I’m working on an extension for Aseprite wherein I’d like to copy a generated string to the clipboard, but I’m unable to use the standard Lua io functions.

io.popen('pbcopy','w'):write(text):close() -- this is not supported, probably for good reason

Is there any alternative within the Aseprite API for copying text to the clipboard?

Update
I just realized console text can be copied. This is good enough for what I’m making.

1 Like

io.popen(‘clip’,‘w’):write(“poop”):close() works (on windows)

1 Like

thanks for the tips! I wish the API let me access the clipboard. here’s my duct-taped-together solution. it attempts to be cross-platform, but I don’t know a simple way to detect the user’s platform, so they’ll have to edit the “autocopy” variable after installing the script, which is unfortunate. anyway:

-- change this variable! valid options are:
--   "windows"  -- if your machine is a windows desktop
--   "osx"      -- if your machine is an apple mac desktop
--   "linux"    -- if your machine is a linux desktop (uses `xsel --clipboard`)
--   "manual"   -- anything else; you must manually press ctrl-c to copy the output
local autocopy = "linux"


-- copy string to host clipboard
-- https://community.aseprite.org/t/solved-copy-string-within-aseprite-extension/16344
local function copy_to_clipboard(str)

  -- returns whether the copy command succeeded
  -- (this function may fail; use pcall)
  local function os_copy(os_name,text)
    if os_name=="windows" then
      return io.popen('clip','w'):write(text):close()
    elseif os_name=="osx" then
      return io.popen('pbcopy','w'):write(text):close()
    elseif os_name=="linux" then
      return io.popen('xsel --clipboard','w'):write(text):close()
    end
    return nil --failed
  end

  local pcall_ok,os_copy_ok = pcall(os_copy,autocopy,str)
  if not (pcall_ok and os_copy_ok) then
    -- autocopy=="manual", or some failure
    -- fallback to print() (can be manually copied with ctrl-c)
    print(str)
  end
end

--copy_to_clipboard("hello world")

(and here it is in-context for an export script I wrote)