Jump to content

Determining File extension type on import


mfl

This topic is 5054 days old. Please don't post here. Open a new topic instead.

Recommended Posts

I need to determine the types of pic files a user is importing into our app.

We only allow "Insert Picture(Reference)" into our DB because we don't want to jam up the DB.

The reason being is that some people have been importing "peg", "PDF" files instead of valid "jpg or jpeg" files. When our china users try to access the style on their windows machine, because we do a refresh of all pics, we get the dreaded white screen of app freeze which needless to say frustrates them.

The current script statements that the other "developer" put in doesn't work because it references an empty container field."see code...'

I need to know this at the Insert Picture script so that I can kick them out if they have chosen the wrong type.


04  Import & Export: 04-0190 > Import Art: Art Department Entry 

Show Custom Dialog [ Title: "IMPORT IMAGE & CREATOR FILE"; Message: "Import " & Get (ScriptParameter) & " JPG and creator 

file?"; Buttons: “OK”, “Cancel” ] 

If [ Get (LastMessageChoice) = 2 ] 

Exit Script [  ] 

End If 

Freeze Window 

Set Error Capture [ On ] 

#Check for Mac or PC 

If [ Middle( Get (DesktopPath);2;1) = "U" or Middle( Get (DesktopPath);2;1) = "m" or Middle( Get (DesktopPath);2;1) = "M"  // Mac ] 

Set Variable [ $filepathserver; Value:"filemac:/" ] 

Set Variable [ $imagepathserver; Value:"imagemac:/" ] 

End If 

If [ Middle( Get (DesktopPath);2;1) = "C"  //  PC 

 ] 

Set Variable [ $filepathserver; Value:"filewin://DEEPTHOUGHT/" ] 

Set Variable [ $imagepathserver; Value:"imagewin://DEEPTHOUGHT/" ] 

End If 

#Set Variables and get script parameter 

Set Error Capture [ On ] 

Set Variable [ $section; Value:GetValue ( Get ( ScriptParameter ) ; 1 ) ] 

Set Variable [ $StyleCode; Value:Style_Main::Style_ID_Style_Code ] 

Set Variable [ $StyleSN; Value:Style_Main::Style_ID_Serial_Number ] 

Set Variable [ $ProgramCode; Value:Style_Main::Style_ID_Program_Code ] 

Set Variable [ $ProgramName; Value:Programs::Program_Name ] 

Set Variable [ $ProgramNumber; Value:Style_Main::Style_ID_Program_SN ] 

Set Variable [ $action; Value:"Import" ] 

#BW Front JPG 

If [ $section = "BW Front" ] 

Show Custom Dialog [ Title: "IMPORT JPG"; Message: "Import " & Get (ScriptParameter) & " JPG."; Buttons: “OK” ] 

Set Field [ Style_Main::Style_Graphic_3_Count_BW_Front; Style_Main::Style_Graphic_3_Count_BW_Front + 1 ] 

Set Variable [ $version; Value:Style_Main::Style_Graphic_3_Count_BW_Front ] 

Go to Field [ Style_Main::Style_Graphic_JPG_3_BW_Front ] 

Insert Picture [  ] 

[ Reference ] 

Set Variable [ $filepathjpg; Value:$filepathserver & "MondettaEnterprise/Groups/ERP/Documents/Programs/" 

& Style_Main::Style_ID_Program_SN & "/" 

& Style_Main::Style_ID_Program_SN & "_Art_BW/" 

& Style_Main::Style_ID_Style_Code & "_BW_Front_" 

& Style_Main::Style_Graphic_3_Count_BW_Front 

& "." & Case ( 

If ( Middle ( ( Right ( GetValue ( Style_Main::Style_Graphic_3_Path_JPG_BW_Front_Sketch ; 3 ) ; 5 )); 1; 1 ) ; "."; Right 

( GetValue ( Style_Main::Style_Graphic_3_Path_JPG_BW_Front_Sketch ; 3 ) ; 5 )) ; 

If ( Middle ( ( Right ( GetValue ( Style_Main::Style_Graphic_3_Path_JPG_BW_Front_Sketch ; 3 ) ; 5 )); 2; 1 ) ; "."; Right 

( GetValue ( Style_Main::Style_Graphic_3_Path_JPG_BW_Front_Sketch ; 3 ) ; 4 )) ; 

If ( Middle ( ( Right ( GetValue ( Style_Main::Style_Graphic_3_Path_JPG_BW_Front_Sketch ; 3 ) ; 5 )); 3; 1 ) ; "."; Right 

( GetValue ( Style_Main::Style_Graphic_3_Path_JPG_BW_Front_Sketch ; 3 ) ; 3 )) ) ] 

#> Require File validation here to stop user from importing non JPEG files. 

//  If [ Right ( Style_Main::Style_Graphic_JPG_3_BW_Front ; 3 ) ≠ "jpg" or Right ( Style_Main::Style_Graphic_JPG_3_BW_Front ; 

3 ) ≠ "jpeg" ] 

//  Show Custom Dialog [ Title: "INCORRECT FILE TYPE"; Message: "You can only use JPG's, please try again."; Buttons: 

“OK” ] 

//  Clear [ Style_Main::Style_Graphic_JPG_3_BW_Front ] 

[ Select ] 

//  Exit Script [  ] 

//  End If 

Set Variable [ $imagemacjpg; Value:$imagepathserver & "MondettaEnterprise/Groups/ERP/Documents/Programs/" 

& Style_Main::Style_ID_Program_SN & "/" 

& Style_Main::Style_ID_Program_SN & "_Art_BW/" 

& Style_Main::Style_ID_Style_Code & "_BW_Front_" 

& Style_Main::Style_Graphic_3_Count_BW_Front 

& "." & Case ( 

If ( Middle ( ( Right ( GetValue ( Style_Main::Style_Graphic_3_Path_JPG_BW_Front_Sketch ; 3 ) ; 5 )); 1; 1 ) ; "."; Right 

( GetValue ( Style_Main::Style_Graphic_3_Path_JPG_BW_Front_Sketch ; 3 ) ; 5 )) ; 

If ( Middle ( ( Right ( GetValue ( Style_Main::Style_Graphic_3_Path_JPG_BW_Front_Sketch ; 3 ) ; 5 )); 2; 1 ) ; "."; Right 

( GetValue ( Style_Main::Style_Graphic_3_Path_JPG_BW_Front_Sketch ; 3 ) ; 4 )) ; 

If ( Middle ( ( Right ( GetValue ( Style_Main::Style_Graphic_3_Path_JPG_BW_Front_Sketch ; 3 ) ; 5 )); 3; 1 ) ; "."; Right 

Set Variable [ $imagemacjpg; Value:$imagepathserver & "MondettaEnterprise/Groups/ERP/Documents/Programs/" 

& Style_Main::Style_ID_Program_SN & "/" 

& Style_Main::Style_ID_Program_SN & "_Art_BW/" 

& Style_Main::Style_ID_Style_Code & "_BW_Front_" 

& Style_Main::Style_Graphic_3_Count_BW_Front 

& "." & Case ( 

If ( Middle ( ( Right ( GetValue ( Style_Main::Style_Graphic_3_Path_JPG_BW_Front_Sketch ; 3 ) ; 5 )); 1; 1 ) ; "."; Right 

( GetValue ( Style_Main::Style_Graphic_3_Path_JPG_BW_Front_Sketch ; 3 ) ; 5 )) ; 

If ( Middle ( ( Right ( GetValue ( Style_Main::Style_Graphic_3_Path_JPG_BW_Front_Sketch ; 3 ) ; 5 )); 2; 1 ) ; "."; Right 

( GetValue ( Style_Main::Style_Graphic_3_Path_JPG_BW_Front_Sketch ; 3 ) ; 4 )) ; 

If ( Middle ( ( Right ( GetValue ( Style_Main::Style_Graphic_3_Path_JPG_BW_Front_Sketch ; 3 ) ; 5 )); 3; 1 ) ; "."; Right 

( GetValue ( Style_Main::Style_Graphic_3_Path_JPG_BW_Front_Sketch ; 3 ) ; 3 )) ) ] 

Export Field Contents [ Style_Main::Style_Graphic_JPG_3_BW_Front; “$filepathjpg” ] 

Clear [ Style_Main::Style_Graphic_JPG_3_BW_Front ] 

[ Select ] 

Go to Field [ Style_Main::Style_Graphic_JPG_3_BW_Front ] 

Insert Picture [ “$imagemacjpg” ] 

[ Reference ] 

Insert File [ Style_Main::Style_Graphic_3_File_JPG_BW_Front; “$filepathjpg” ] 

[ Reference ] 

#BW Front File 

Show Custom Dialog [ Title: "IMPORT JPG"; Message: "Import " & Get (ScriptParameter) & " creator file."; Buttons: “OK” ] 

Set Variable [ $version; Value:Style_Main::Style_Graphic_3_Count_BW_Front ] 

Clear [ Style_Main::Style_Graphic_3_Creator_BW_Front ] 

[ Select ] 

Go to Field [ Style_Main::Style_Graphic_3_Creator_BW_Front ] 

Insert Picture [  ] 

[ Reference ] 

Set Variable [ $filepathfile; Value:$filepathserver & "MondettaEnterprise/Groups/ERP/Documents/Programs/" 

& Style_Main::Style_ID_Program_SN & "/" 

& Style_Main::Style_ID_Program_SN & "_Art_BW/" 

& Style_Main::Style_ID_Style_Code & "_BW_Front_File_" 

& Style_Main::Style_Graphic_4_Count_BW_Back 

& "." & Case ( 

If ( Middle ( ( Right ( GetValue ( Style_Main::Style_Graphic_3_Path_Creator_BW_Front_Path ; 3 ) ; 5 )); 1; 1 ) ; "."; Right 

( GetValue ( Style_Main::Style_Graphic_3_Path_Creator_BW_Front_Path ; 3 ) ; 5 )) ; 

If ( Middle ( ( Right ( GetValue ( Style_Main::Style_Graphic_3_Path_Creator_BW_Front_Path ; 3 ) ; 5 )); 2; 1 ) ; "."; Right 

( GetValue ( Style_Main::Style_Graphic_3_Path_Creator_BW_Front_Path ; 3 ) ; 4 )) ; 

If ( Middle ( ( Right ( GetValue ( Style_Main::Style_Graphic_3_Path_Creator_BW_Front_Path ; 3 ) ; 5 )); 3; 1 ) ; "."; Right 

( GetValue ( Style_Main::Style_Graphic_3_Path_Creator_BW_Front_Path ; 3 ) ; 3 )) ) ] 

Set Variable [ $imagemacfile; Value:$imagepathserver & "MondettaEnterprise/Groups/ERP/Documents/Programs/" 

& Style_Main::Style_ID_Program_SN & "/" 

& Style_Main::Style_ID_Program_SN & "_Art_BW/" 

& Style_Main::Style_ID_Style_Code & "_BW_Front_File_" 

& Style_Main::Style_Graphic_4_Count_BW_Back 

& "." & Case ( 

If ( Middle ( ( Right ( GetValue ( Style_Main::Style_Graphic_3_Path_Creator_BW_Front_Path ; 3 ) ; 5 )); 1; 1 ) ; "."; Right 

( GetValue ( Style_Main::Style_Graphic_3_Path_Creator_BW_Front_Path ; 3 ) ; 5 )) ; 

If ( Middle ( ( Right ( GetValue ( Style_Main::Style_Graphic_3_Path_Creator_BW_Front_Path ; 3 ) ; 5 )); 2; 1 ) ; "."; Right 

( GetValue ( Style_Main::Style_Graphic_3_Path_Creator_BW_Front_Path ; 3 ) ; 4 )) ; 

If ( Middle ( ( Right ( GetValue ( Style_Main::Style_Graphic_3_Path_Creator_BW_Front_Path ; 3 ) ; 5 )); 3; 1 ) ; "."; Right 

( GetValue ( Style_Main::Style_Graphic_3_Path_Creator_BW_Front_Path ; 3 ) ; 3 )) ) ] 

Export Field Contents [ Style_Main::Style_Graphic_3_Creator_BW_Front; “$filepathfile” ] 

Insert File [ Style_Main::Style_Graphic_3_Creator_BW_Front; “$filepathfile” ] 

[ Reference ] 

End If

Thanks

Michael

Link to comment
Share on other sites

So I just tried another otption by calling another script and passing the created file path, because this actually had the file extension at the end, but not sure why it didn't pick it out.

This is the new script..


04  Import & Export: 04-0330 > Image Check 

Set Variable [ $importfilepath; Value:Get ( ScriptParameter ) ] 

Set Variable [ $period; Value:Position ( $importfilepath ; "." ; 1 ; 1 ) ] 

//  If [ $period + 1  ≠ "jpg" ] 

If [ Right ( $importfilepath ; 3 ) ≠ "jpg" or Right ( $importfilepath ; 4 ) ≠ "jpeg" ] 

//  If [ Middle ( $period ; $period + 1 ; 3 ) ≠ "jpg" ] 

Show Custom Dialog [ Title: "Wrong File type"; Message: "You are trying to import a NON JPEG file, Please try again. 

Thankyou"; Buttons: “OK”, “Cancel” ] 

Exit Script [  ] 

End If 



Link to comment
Share on other sites

The actually file path that is created in the parent script that we store in the container field.

The reason we have to do this is that we have users in china on windows machines and we are on Macs and as we don't store the pics in the DB we need to create these file paths.

Not my design.

Picture is what would be stored.

Picture_3.png

Link to comment
Share on other sites

The actually file path that is created in the parent script that we store in the container field.

Could you check with the Data Viewer ?

( or a Custom Dialog ? )

04 :) Import & Export: 04-0330 > Image Check

Show Custom Dialog [ Get ( ScriptParameter ) ]

...

...

Link to comment
Share on other sites

Actually I have a habit of running my scripts through debugger with the data viewer also open so I can see whats going on, so ya this is whats coming into the child script.

Link to comment
Share on other sites

The only thing that I can suggest is to re-write the step:

If [ Right ( $importfilepath ; 3 ) ≠ "jpg" or Right ( $importfilepath ; 4 ) ≠ "jpeg" ]

Pay attenction to the $var or write directly:

If [ Right ( Get ( ScriptParameter ) ; 3 ) ≠ "jpg" or Right ( Get ( ScriptParameter ) ; 4 ) ≠ "jpeg" ]

Link to comment
Share on other sites

This expression:


Right ( $importfilepath ; 3 ) ≠ "jpg" or Right ( $importfilepath ; 4 ) ≠ "jpeg"

will ALWAYS evaluate as true. Try using AND instead of OR.

See also:

http://fmforums.com/forum/showtopic.php?tid/189757/post/265392/#265392

Link to comment
Share on other sites

... not to count that we are looking for TRUE when the extension is different from jpg/jpeg.

If [ not ( Right ( Get ( ScriptParameter ) ; 3 ) = "jpg" or Right ( Get ( ScriptParameter ) ; 4 ) = "jpeg" ) ]

That was a subtle thing... I think that I may be wrong other times.

Link to comment
Share on other sites

A simple OR would also work - they cannot be both true at the same time.

Simple "OR", what do you mean by this.

Oh by the way the "AND" worked like a charm thanks.

Unfortunately the extension checking has uncovered another major issue when creating the file path. If a file has an extension of "JPG" then no problem the path is created/imported, but if its "JPEG" the path create chops of the "J" and you get an extension of "PEG" which due to the checking will now not go through.

So I'm going to have to modify that bit of script, or get the guy who wrote it to make the change.

I'll save that for another post.

Thanks again

Link to comment
Share on other sites

You can the extract the extension by =

Let ( [

len = Length ( FileName ) ;

pos = Position ( FileName ; "." ; len ; -1 )

] ;

Case ( pos ; Right ( FileName ; len - pos ) )

)

Simple "OR", what do you mean by this.

I meant there was no need to use XOR in Daniele's example. OR is simpler, since it can exit after finding the first proposition true.

Link to comment
Share on other sites

You can the extract the extension by =

Let ( [

len = Length ( FileName ) ;

pos = Position ( FileName ; "." ; len ; -1 )

] ;

Case ( pos ; Right ( FileName ; len - pos ) )

)

So if I understand this correctly I have a field with say 121 characters and the extension is "JPG". Position would find "." at position 118. The case statement would resolve to 3. I would then have to script to pickup the last 3 places of the field or?

Where is the "3" stored, in "pos"?

Thanks

Michael

Link to comment
Share on other sites

The case statement would resolve to 3.

No, the Case statement would return "JPG".

Where is the "3" stored, in "pos"?

As Daniele explained, the expression:

Right ( FileName ; len - pos )

would evaluate in your example as:

Right ( FileName ; 121 - 118 )

Link to comment
Share on other sites

  • 2 weeks later...

Thanks Daniele and comment for your help.

The solution works but seems our users are quite creative when uploading files to the server. Instead of saving PhotoShop files as JPG they just change the file extension. As soon as it hits windows it craps the FM app. So its just becomes more work for them when they have to redo the files the proper way. They will learn, maybe.

Thanks again

Michael

Link to comment
Share on other sites

On a Mac there is the background application "Image Events", and its similar command line counterpart "sips." The following uses sips and returns the graphic format of the file (hopefully). I don't have any inserted without an extension, and FileMaker will not let me Insert one.

You'd need to coerce a FileMaker file path, used to Export Field Contents, to a safe Unix style file path (instead of the choose file line I'm using below, so you can try the routine in AppleScript Script Editor. You would run this via the Perform AppleScript command.

You can use Abs ( Get (SystemPlatform)) = 1 // Mac, to fork your script steps

-- denotes a comment line in AppleScript

-- choose file, returns Mac alias path; coerce to safe Unix path

set img to quoted form of POSIX path of (choose file)

-- "example: '/Users/fej/Downloads/zMisc/PDFs images/Google_Truck' "

-- the above is the filepath syntax the sips command needs

set imgFormat to do shell script "sips -g format " & img & " | tail -n 1 | cut -d ':' -f 2 | cut -c 2-"

-- "png"

Link to comment
Share on other sites

This topic is 5054 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.