Combining save dialog with databaseexportusingformula


#1

I have this:

local theExportName,theFileChoice
theExportName=datepattern(today(),“MM_DD_YYYY”)+"_inv_update.csv"

databaseexportusingformula “/Users/barrykahn/Desktop/”+theExportName,“LF”,“replace(csvquoted(str(«ItemID»)+{})+{,}+csvquoted(«ProductStatus»+{})+{,}+csvquoted(str(«NumberInStock»)+{}),cr(),{ })”,“ItemID,ProductStatus,NumberInStock”

which saves to the desktop of my home Mac.

I’d like to retain the export format but allow the user to choose where to save the file. I suspect there’s a way to modify the above so that the user can choose, some version of this:

local fileChoice
savedialog fileChoice
export fileChoice,exportline()

but my experiments are (as usual) not getting anywhere.

thanks
b


#2

Use the choosefolderdialog statement to get the path to save the export.

Note to Jim: Using the Copy Topic URL from the Topic menu in the Help file gives a notice that the path has been saved but it never actually is put on the clipboard. This is with the just downloaded new Pan X version.


#3

I don’t think I’m explaining well. Might be the distraction of 4-8 inches of snow arriving tonight and it’s only mid-November. Cripes.

I want to use a regular savedialog (so I can choose where to save the file) but retain the file name which this creates:

local theExportName,theFileChoice
theExportName=datepattern(today(),“MM_DD_YYYY”)+"_inv_update.csv"

and also export the specific fields listed in the databaseexportusingformula.

thanks
b


#4

Yes, Gary gave you the correct answer, he just didn’t write the actual code for you. You’ll have to plug the result of choosefolderdialog into your code.

local targetFolder
choosefolderdialog targetFolder
if targetFolder="" return endif
let theExportName=datepattern(today(),"MM_DD_YYYY")+"_inv_update.csv"
databaseexportusingformula targetFolder+"/"+theExportName,"LF",…

For more flexibility you might want to use the more modern 'choosefiledialog` statement, which has options that allow you to specify the initial path, the title, button name, etc.

local targetFolder
choosefiledialog targetFolder,"folders",true(),"filetypes",".","title","Choose Export Folder"

If you don’t need any of those additional options then * choosefolderdialog* is simpler.

Doh! Wish I’d known about that a few hours ago. I’ve gone ahead and fixed that, but you probably won’t see that fix for quite some time. In the meantime, you can get the URL by right clicking on the box in the upper left hand corner of the Help window (where the sample of the current statement or function is shown). Right clicking is how I always do it, so I never noticed that the menu item didn’t work.


#5

Thank you both, again. Tomorrow, after the storm passes, I will look up “let” in the help file. I may have seen it before in passing, but I’m quite sure I’ve never tried to use it. Every day something new and useful!

b


#6
let theExportName=datepattern(today(),"MM_DD_YYYY")+"_inv_update.csv"

is equivalent to

Local theExportName
theExportName=datepattern(today(),"MM_DD_YYYY")+"_inv_update.csv"

It declares the local variable, if it doesn’t already exist, and assigns a value to it.


#7

Nice. Now, to push my luck: is there any way for Pan to track and report where the file was just saved? Something like:

Message “File named xxxxxxxxxxx was just saved to \Users\bkahn\Desktop” ?

b


#8

Just add some more code to include your variables with that info.

if (fileexists(targetFolder+"/"+theExportName)=false())
	alert "Export failed to save file at designated path!"
else
	message "File named "+theExportName+" was just saved to "+targetFolder+"."
endif

This will first check to make sure the exported file has been created and if it has it will then give the message with the name and location.


#9

The file saved in the chosen location but no message appeared. I tried this, changing false to true:

if (fileexists(targetFolder+"/"+theExportName)=true())
message “File named “+theExportName+” was just saved to “+targetFolder+”.”
else
message “File failed to save. Oh, no!”
endif

…and it worked (although since it worked I don’t know if the ‘File failed to save’ message would actually show up if it failed… Why your version didn’t work is beyond me. I’ll test it again when I have time.

thanks
b


#10

Oops, my mistake. That should have been using alertok instead of alert. The alert statement has two prameters instead of one as used with the alertok. This means that the buttons parameter was missing and the text message was put in its place.


#11

Yep!

thanks
b


#12

Personally in situations like this I usually like to use nsnotify instead of message. That way I get the notification, but it automatically disappears after a few seconds – I don’t have to click to dismiss it. This is just a personal preference on my part, feel free to use message if you like that better (I know that some people don’t like notifications at all).