I have used save() in my code to save images of the window to the sketch folder as a ‘screenshot’ function and have exported my program to an application.
When I run the application and tried to screenshot the image is not saved.
I have also tried saving the images to a data folder and adding a data folder to the application folder but this also doesn’t work.
Is it possible to save images from the application?
It sounds like you might be trying to saving files inside the app itself. What OS are you building / testing on? Have you tried a relative path outside the sketch, or an absolute path like the user Documents folder?
Thanks for your reply, it works when I use an absolute path however doesn’t seem to work using a relative path. I will be distributing the app to other people so I don’t know how this will work if I can only use an absolute path? I am on macOS.
Thank you for this. I forgot to say I am using Python mode and have converted your code to Python which works when running it in Processing but doesn’t work when I export it to an app. It seems that the sketchPath() is not defined properly for the Python app as when I print it out it just returns ‘/’. I’m not sure why this is happening, do I need to set the sketchPath() somewhere?
I’m not sure why but when I exported the app with ‘Full Screen (Present Mode)’ selected, sketchPath() didn’t work but when I exported the app without this chosen, it worked.
Thanks for your help!
Considering your intended goal of exporting a distributable app, you might want to consider using selectFolder() instead of trying to find a path, so the user can select the save path themselves:
savedir = None
frame=0
def setup():
size(800,800)
selectFolder("Select a directory to save to", "fileSelected")
def fileSelected(selection):
global savedir
println(selection.getAbsolutePath())
savedir = selection.getAbsolutePath()
def draw():
# selectFolder is async so frameCount won't work
global frame
global savedir
background(0)
if savedir is not None:
frame=1
if (frame==1):
background(255,0,0)
text(savedir,20,20)
saveFrame(savedir + "/data/red-data.png")
frame+=1
if (frame==2):
background(0,255,0)
text(savedir,20,20)
saveFrame(savedir + "/green-inside.png")
frame+=1
if (frame==3):
background(0,0,255)
text(savedir,20,20)
saveFrame(savedir + "/blue-abs-inside.png")
frame+=1
Here’s another alternative that uses a platform independent way to generate a save path:
from os.path import expanduser
savedir = None
frame=0
def setup():
global savedir
size(800,800)
savedir = getSaveDir()
println(savedir)
def draw():
# selectFolder is async so frameCount won't work
global frame
global savedir
background(0)
if savedir is not None:
frame=1
if (frame==1):
background(255,0,0)
text(savedir,20,20)
saveFrame(savedir + "/data/red-data.png")
frame+=1
if (frame==2):
background(0,255,0)
text(savedir,20,20)
saveFrame(savedir + "/green-inside.png")
frame+=1
if (frame==3):
background(0,0,255)
text(savedir,20,20)
saveFrame(savedir + "/blue-abs-inside.png")
frame+=1
def getOsVersion():
ver = sys.platform.lower()
if ver.startswith('java'):
import java.lang
ver = java.lang.System.getProperty("os.name").lower()
return ver
def getSaveDir():
savedir=expanduser("~")
ver=getOsVersion()
if (ver == "mac os x"):
savedir+="/Documents/sketchPathTest"
# Other os tests go here
return savedir