Rename a file to the contents of a field on a card

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
trags3
Posts: 432
Joined: Wed Apr 09, 2014 1:58 am

Rename a file to the contents of a field on a card

Post by trags3 » Mon Feb 01, 2021 4:38 am

I have a field CaseNumber.
I generate a pdf File and save it in the Documents Folder.
I want to have the name of the PDF be the contents of the CaseNumber Field
I know this is easy but I have been away from LC for about a year.
Help Please!

Tom

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10048
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Rename a file to the contents of a field on a card

Post by FourthWorld » Mon Feb 01, 2021 5:00 am

Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Xero
Posts: 157
Joined: Sat Jun 23, 2018 2:22 pm

Re: Rename a file to the contents of a field on a card

Post by Xero » Mon Feb 01, 2021 1:37 pm

If you want to put the newly created file name somewhere on the page when it's printed (like the old fashioned TXT files on windows used to do), you could try something like this:

Code: Select all

on mousedown --or mouseup, whatever you prefer--
  ask file "Choose a file for the PDF:" 
      if it is not empty then 
         put IT & ".pdf" into tTargetFile
         put tTargetFile into field "CaseNumber" --of card CardName, or whatever you need to do to get it to the right place. If you want the whole file path, you'll need to use something like longfilepath--
         open printing to pdf tTargetFile
         set the printpaperorientation to "landscape" -- or "Portrait", whatever works for your project--
         set the printscale to 0.85 --I find I need to tweak this to get it to work for the card and page size discrepancies--
         set the printmargins to 10,10,10,10 --Or whatever margins you want/ need to get it to work--
         print card CardName --put your card's name here--
         close printing
      end if
 end mousedown --or mouseup--
 
I haven't tried that, but it should work. It just intercepts the filename you are saving and puts it into a field before you print.

XdM

trags3
Posts: 432
Joined: Wed Apr 09, 2014 1:58 am

Re: Rename a file to the contents of a field on a card

Post by trags3 » Mon Feb 01, 2021 2:52 pm

Hi Richard,
Thank You again for your help.

Really I want the name of the file to be the contents of a field on the card.
Here is my code:

on mouseUp
put fld"ClaimNumber" into tNewFIle --in this case the data in field ClaimNumber is TR12345 -- I want the name of the generated PDF to be TR12345.pdf
put specialFolderPath("Documents") & "/tNewFile.pdf" into tFileToPrint --I think my problem is here. I have tried numerous variations to no avail
open printing to pdf tFileToPrint --no matter what I have made the generated PDF is named tNewFile.pdf
print cd"ClaimInfo"
Close printing

Thanks Again,
Tom

trags3
Posts: 432
Joined: Wed Apr 09, 2014 1:58 am

Re: Rename a file to the contents of a field on a card

Post by trags3 » Mon Feb 01, 2021 2:58 pm

I do have "end mouseUp" at the end

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Rename a file to the contents of a field on a card

Post by bogs » Mon Feb 01, 2021 3:19 pm

trags3 wrote:
Mon Feb 01, 2021 2:52 pm
put specialFolderPath("Documents") & "/tNewFile.pdf" into tFileToPrint --I think my problem is here.
I think your right ;)

Try changing it to -

Code: Select all

put (specialFolderPath("Desktop") & "/LcForumTests/printTesting/" & tNewFile & ".pdf") into tFileToPrint --tNewFile is a variable, as such, it should be outside of the quoted file name...
...and see if that helps
Image

trags3
Posts: 432
Joined: Wed Apr 09, 2014 1:58 am

Re: Rename a file to the contents of a field on a card

Post by trags3 » Mon Feb 01, 2021 3:46 pm

Sorry Bogs,
No file is printed and no folders are generated. It's just like kissing your sister.... Nothing Happens.
Tom

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Rename a file to the contents of a field on a card

Post by bogs » Mon Feb 01, 2021 3:54 pm

My bad, I realized after re-reading the code after your reply that I still had my folderpath (with folders already made) in place heh. It should have read ~

Code: Select all

put (specialFolderPath("Documents") & "/" & tNewFile & ".pdf") into tFileToPrint 
to use your example.
Image

trags3
Posts: 432
Joined: Wed Apr 09, 2014 1:58 am

Re: Rename a file to the contents of a field on a card

Post by trags3 » Mon Feb 01, 2021 4:00 pm

Hi Bogs!
Oh Happy Day! The PDF generated is correct and named TR12345.PDF Just as I need!

Thanks again for your help!
Tom

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Rename a file to the contents of a field on a card

Post by bogs » Mon Feb 01, 2021 4:14 pm

No problems, glad it worked out for you :)
Image

bobcole
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 161
Joined: Tue Feb 23, 2010 10:53 pm

Re: Rename a file to the contents of a field on a card

Post by bobcole » Mon Feb 01, 2021 11:33 pm

Tom:
There is also a "open printing to pdf" command in the Dictionary with some sample code.
Maybe that would be useful.
Bob

bobcole
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 161
Joined: Tue Feb 23, 2010 10:53 pm

Re: Rename a file to the contents of a field on a card

Post by bobcole » Mon Feb 01, 2021 11:36 pm

Oops. Never mind. You are using that command.
I didn't read the prior messages closely enough.
Sorry, Bob

Post Reply